noloader / cryptopp-cmake

CMake files for Crypto++ project
BSD 3-Clause "New" or "Revised" License
92 stars 68 forks source link

MSVC static runtime library #63

Closed jsutes closed 2 years ago

jsutes commented 3 years ago

According to https://www.cryptopp.com/wiki/Visual_Studio cryptopp should favor static linking against the C/C++ runtime (/MT or /MTd).

When I use this cmake project with visual studio or ninja generator, I keep getting dynamically linked runtime. You can test this by searching the build.ninja output file for "\MD" or "\MT".

My cmake command is

cmake -G Ninja -DBUILD_SHARED=OFF -DBUILD_STATIC=ON -DBUILD_TESTING=OFF -DCMAKE_INSTALL_PREFIX=C:\cryptopp -DCMAKE_BUILD_TYPE=Release ..

How can we switch between static and dynamic linking with this project? According to https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html#variable:CMAKE_MSVC_RUNTIME_LIBRARY and https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html we should be able to use CMAKE_MSVC_RUNTIME_LIBRARY as a global variable or MSVC_RUNTIME_LIBRARY as a target specific property to choose the desired option. I tried setting these variables to "MultiThreaded" but it did not have an effect (I do successfully use these variables in my own projects).

I was able to do a find-and-replace "/MD" to "/MT" in the build.ninja file and successfully built with static linked runtime library, so there shouldn't be issues with the build. We just need to figure out how to switch between the two in cmake instead of having to manually edit the ninja or visual studio output file.

OS: Windows 10 Compiler: MSVC/14.28.29333 CMake: 3.19

CleanHit commented 3 years ago

@jsutes Here is a minimal example of my CMakeLists.txt file. With it I download and build the cryptopp static library and link it to my project using Visual Studio Community 2019s x86_amd64 MSCV compiler in Visual Studio Code in Windows. Using MinGWs x64 GCC compiler in Windows was just a pain in the $%&.

Unfortunately, I can't build the cryptopp shared library in Windows, in Linux everything works fine with GCC. When I change

set(BUILD_SHARED OFF CACHE INTERNAL "Build CryptoPP SHARED libraries")

To

set(BUILD_SHARED ON CACHE INTERNAL "Build CryptoPP SHARED libraries")

I get this build error build_cryptopp_shared_error.txt Does anyone know what cryptopp-cmake CMake variable I should set to build the shared library?

My environment info: OS: Windows 10 IDE: Visual Studio Code Compiler: MSVC/14.28.29333 CMake: 3.19.1

noloader commented 3 years ago

using MinGWs x64 GCC compiler in Windows was just a pain in the $%&.

CMake is a royal pain in the $%&.

Here is my CMakeFiles.txt.

You can zip CMakeFiles.txt and then drag-and-drop the zip file in the bug report to make it easy to download.

CleanHit commented 3 years ago

using MinGWs x64 GCC compiler in Windows was just a pain in the $%&.

CMake is a royal pain in the $%&.

Here is my CMakeFiles.txt.

You can zip CMakeFiles.txt and then drag-and-drop the zip file in the bug report to make it easy to download.

You are right and I've updated my post.

jsutes commented 3 years ago

Thanks for the comments. I've continued building cryptopp statically with dynamic linking against the C/C++ runtime even though it is against the advice of https://www.cryptopp.com/wiki/Visual_Studio. It's actually made my builds easier since other third party libraries I use get built with dynamic C/C++ runtime by default.

@CleanHit

I get this build error build_cryptopp_shared_error.txt Does anyone know what cryptopp-cmake CMake variable I should set to build the shared library?

So far when I want to use cryptopp as a shared library, I've been using vcpkg. You say your compiler is MSVC but your error txt file looks like your using mingw32-make as the build tool. Is that allowed? I've only used Ninja or Visual Studio as the build tool with MSVC and CMake.

In summary, I'm fine if this issue is closed since it's no longer a problem for me. But maybe it should be kept open until an option is added to toggle static/dynamic C/C++ runtime linking.

CleanHit commented 3 years ago

So far when I want to use cryptopp as a shared library, I've been using vcpkg. You say your compiler is MSVC but your error txt file looks like your using mingw32-make as the build tool. Is that allowed? I've only used Ninja or Visual Studio as the build tool with MSVC and CMake.

Thanks for point that out. It was the remaining of my attempts to compile using _MSYS2_s GNU compiler with mingw-w64-x86_64-cmake, mingw-w64-x86_64-make, mingw-w64-x86_64-gcc, mingw-w64-x86_64-gdb and mingw-w64-x86_64-clang packages. The only thing that was left from that setting was mingw-w64-x86_64-cmake and mingw-w64-x86_64-make packages. I've removed that mow and set the CMake standalone installation for Windows to be used.

I've only used Ninja or Visual Studio as the build tool with MSVC and CMake.

I couldn't make it compile using Ninja in Visual Studio Community 2019 and Visual Studio Code, I was getting the error ...CMakeFiles\rules.ninja:293: expected '=', got lexing error out\build\x64-Release.... I had to set my CMake generator to "generator": "Visual Studio 16 2019 Win64" in Visual Studio Community 2019 or to "cmake.generator": "Visual Studio 16 2019" in global VSCode user settings.json file.

If I try to build cryptopp-shared.lib in Visual Studio Community 2019 then the file is not being built and I get:

Severity Code Description Project File Line Suppression State Error LNK1104 cannot open file '_deps\cryptopp-build\Debug\cryptopp-shared.lib'  <path_to_project>\out\build\x64-Debug (Standard)\hmmenc_client <path_to_project>\out\build\x64 Debug (Standard)\LINK 1

Only cryptopp-shared.dll, cryptopp-shared.ilk and cryptopp-shared get created in \out\build\x64-Debug (Standard)_deps\cryptopp-build\Debug

noloader commented 2 years ago

Crypto++ DLL is probably not going to work. Under Visual Studio it is actually a FIPS DLL and it is a lot of trouble. My advice is, remove anything that has to do with a DLL.

If you want a DLL, then create a Wrapper DLL with the functions you want to use.

With that said, I don't know what the CMake folks are doing. I keep this repository open so there's a central location for CMakeFileList.txt. People can callborate on it, and folks know the place to download it.