LNK 2001 Errors: vcpkg with Static Libraries
jasonabuzzell opened this issue · 7 comments
Describe the bug
I would like to build an .exe through Visual Studio 2022 with AWS SDK for C++ using vcpkg libraries. I can currently do this with DLLs, but I want to instead build the .exe with static .libs. After doing the steps below, I see LNK2001 errors for every AWS function I try to use in the project:
- git clone https://github.com/Microsoft/vcpkg.git
- cd vcpkg
- ./bootstrap-vcpkg.sh
- ./vcpkg integrate install
- ./vcpkg install aws-sdk-cpp
- ./vcpkg install "aws-sdk-cpp[core, s3, lambda]":x64-windows-static --recurse
This does put the expected files (.h, .lib) into vcpkg.
My vcpkg solution settings are as follows:
Then switched over to /MT instead of /MD:
And I also added the .libs as additional dependencies, just in case:
Thank you once again for all of the help! Hopefully this is the last question, and everything behaves!
Expected Behavior
I expect the .exe to be built properly with the vcpkg .libs.
Current Behavior
Severity Code Description Project File Line Suppression State
Error LNK1120 2 unresolved externals AWS Test C:\Users\jason\Desktop\AWS Test\x64\Release\AWS Test.exe 1
Error LNK2001 unresolved external symbol "__declspec(dllimport) void __cdecl Aws::InitAPI(struct Aws::SDKOptions const &)" (__imp_?InitAPI@Aws@@YAXAEBUSDKOptions@1@@Z) AWS Test C:\Users\jason\Desktop\AWS Test\AWS Test\main.obj 1
Error LNK2001 unresolved external symbol "__declspec(dllimport) void __cdecl Aws::ShutdownAPI(struct Aws::SDKOptions const &)" (__imp_?ShutdownAPI@Aws@@YAXAEBUSDKOptions@1@@Z) AWS Test C:\Users\jason\Desktop\AWS Test\AWS Test\main.obj 1
Reproduction Steps
#define USE_IMPORT_EXPORT
#define USE_WINDOWS_DLL_SEMANTICS
#define AWS_SDK_VERSION_MAJOR 1
#define AWS_SDK_VERSION_MINOR 0
#define AWS_SDK_VERSION_PATCH 0
#include <aws/core/Aws.h>
int main(int argc, char** argv)
{
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// make your SDK calls here.
}
Aws::ShutdownAPI(options);
return 0;
}
Possible Solution
This does seem like a common issue that pops up, and I get find a post that is the exact same as mine (#1074). Unfortunately, the first link that is sent as a solution no longer exists, but that may be the solution to this. I'm sure I'm just missing some definition in the vcpkg solution page, but I'm unsure what it is.
Additional Information/Context
No response
AWS CPP SDK version used
Latest (1.11.162)
Compiler and Version used
Visual Studio 17 2022
Operating System and version
Windows 10, Version 22H2
Hi @jasonabuzzell, I'm not an expert on using vcpkg and I'm not sure if you are able to use vcpkg with this sdk and static libraries. As noted in our readme I would recommend opening and issue on the vcpkg repository
The aws-sdk-cpp port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
I can help you build and use this sdk statically from the source on windows. You can do that by following the readme Building From Source. The only change you need to make while following those steps is to add these two arguments to your cmake command -DFORCE_SHARED_CRT=OFF -DBUILD_SHARED_LIBS=OFF
. That will make your cmake command look something like this:
cmake <path-to-root-of-this-source-code> \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_INSTALL_PREFIX=<path-to-install> \
-DBUILD_ONLY="s3" \
-DFORCE_SHARED_CRT=OFF \
-DBUILD_SHARED_LIBS=OFF
Please let me know if you still have any questions/problems with building this sdk statically.
Hi @jmklix, thanks for the help again! When it comes to building the SDK from source, I always get stuck in the same place (unresolved external symbols, LNK 2001). I would like to do this in CMake-less way and instead set it up directly through Visual Studio 2022. My steps are as follows:
1. Get CMake 3.21.7 and add to PATH
2. C:\Users\Jason > git clone --recurse-submodules https://github.com/aws/aws-sdk-cpp
4. C:\Users\Jason > mkdir sdk_build
5. C:\Users\Jason > cd sdk_build
6. C:\Users\Jason\sdk_build > cmake "..\aws-sdk-cpp" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="C:/Program Files (x86)/aws-cpp-sdk-all" -DBUILD_ONLY="s3" -DFORCE_SHARED_CRT=OFF -DBUILD_SHARED_LIBS=OFF
7. C:\Users\Jason\sdk_build > cmake --build . --config=Release
8. C:\Users\Jason\sdk_build > cmake --install . --config=Release
In Visual Studio, setting up the project properties (following #1154):
1. C/C++ - > General -> Additional Include Directories: C:\Program Files\aws-cpp-sdk-all\include;
2. Linker - > General -> Additional Library Directories: C:\Program Files\aws-cpp-sdk-all\lib;
3. Linker - > Input - > Additional Dependencies: aws-cpp-sdk-core.lib; userenv.lib; ws2_32.lib; Wininet.lib; bcrypt.lib; Mincore.lib;
I can see that it recognizes my libraries in the executable folder, it recognizes the code from the header, but it still shows me LNK2001 errors:
We did start a previous thread about it (#2734), but I do have another thread about it that may give you more information on this: #2669
Also, something else I noticed but thought might have been deprecated up until reading this rather recent post (#2421), I don't have a bin
folder, and I'm starting to wonder if that's what I need to link to.
Hi @jmklix! I just wanted to update and close this thread as I found out what the issue was. In turns out, for either method of building the SDK (from source or from vcpkg), I needed more .libs and/or more preprocessor definitions. I don't know if every single one of them will be useful, but hopefully people can use this thread to check for every single permutation.
For Preprocessor Definitions:
AWS_SDK_PLATFORM_WINDOWS
USE_WINDOWS_DLL_SEMANTICS
ENABLE_WINDOWS_CLIENT
ENABLE_BCRYPT_ENCRYPTION
USE_IMPORT_EXPORT
AWS_S3_EXPORTS
AWS_LAMBDA_EXPORTS
AWS_CORE_EXPORTS
NDEBUG
_CONSOLE
For Additional Dependencies (where Additional Library Directories points to the .lib
file location):
aws-cpp-sdk-core.lib
userenv.lib
ws2_32.lib
Wininet.lib
bcrypt.lib
Mincore.lib
version.lib
Advapi32.lib
shlwapi.lib
Hope this helps people in the future!
⚠️ COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
Hi @jmklix! I just wanted to update and close this thread as I found out what the issue was. In turns out, for either method of building the SDK (from source or from vcpkg), I needed more .libs and/or more preprocessor definitions. I don't know if every single one of them will be useful, but hopefully people can use this thread to check for every single permutation.
For Preprocessor Definitions:
AWS_SDK_PLATFORM_WINDOWS USE_WINDOWS_DLL_SEMANTICS ENABLE_WINDOWS_CLIENT ENABLE_BCRYPT_ENCRYPTION USE_IMPORT_EXPORT AWS_S3_EXPORTS AWS_LAMBDA_EXPORTS AWS_CORE_EXPORTS NDEBUG _CONSOLE
For Additional Dependencies (where Additional Library Directories points to the
.lib
file location):aws-cpp-sdk-core.lib userenv.lib ws2_32.lib Wininet.lib bcrypt.lib Mincore.lib version.lib Advapi32.lib shlwapi.lib
Hope this helps people in the future!
hello i have same problem,where Preprocessor Definitions you add to ?your own project vs configuration or aws-cpp-sdk-core?
I had same problem with link functions from static libraries.
Just not define USE_IMPORT_EXPORT when you want to use static libraries. Remove any definition in code before includes of AWS SDK headers.
USE_IMPORT_EXPORT need only for dynamic libraries https://github.com/aws/aws-sdk-cpp/blob/main/docs/SDK_usage_guide.md#build-defines.
Hi @jmklix! I just wanted to update and close this thread as I found out what the issue was. In turns out, for either method of building the SDK (from source or from vcpkg), I needed more .libs and/or more preprocessor definitions. I don't know if every single one of them will be useful, but hopefully people can use this thread to check for every single permutation.
For Preprocessor Definitions:
AWS_SDK_PLATFORM_WINDOWS USE_WINDOWS_DLL_SEMANTICS ENABLE_WINDOWS_CLIENT ENABLE_BCRYPT_ENCRYPTION USE_IMPORT_EXPORT AWS_S3_EXPORTS AWS_LAMBDA_EXPORTS AWS_CORE_EXPORTS NDEBUG _CONSOLE
For Additional Dependencies (where Additional Library Directories points to the
.lib
file location):aws-cpp-sdk-core.lib userenv.lib ws2_32.lib Wininet.lib bcrypt.lib Mincore.lib version.lib Advapi32.lib shlwapi.lib
Hope this helps people in the future!
It's really helpful