The CMake command line is displayed in the Output Window, along with other output from CMake. Visual Studio runs CMake and generates the CMake cache file ( CMakeCache.txt) for the default configuration. The Solution Explorer displays the folder structure and files. Visual Studio adds CMake items to the Project menu, with commands for viewing and editing CMake scripts. When you open a folder containing a CMakeLists.txt file, the following things happen. Both C++ CMake tools for Windows and Linux Development with C++ are required for cross-platform CMake development.įor more information, see Install the C++ Linux workload in Visual Studio. InstallationĬ++ CMake tools for Windows is installed as part of the Desktop development with C++ and Linux Development with C++ workloads. cmake.exe is invoked directly by Visual Studio for CMake configuration and build. CMake project files (such as CMakeLists.txt) are consumed directly by Visual Studio for the purposes of IntelliSense and browsing. Don't define BUILD_DLL in the project using the DLL, and it should work.Visual Studio's native support for CMake enables you to edit, build, and debug CMake projects on Windows, the Windows Subsystem for Linux (WSL), and remote systems from the same instance of Visual Studio. Then it should generate a power.lib file when the MSVC compiler and a power.a when using MINGW. In the project building the DLL power.dll with CMake, you should define the symbol BUILD_DLL: add_definitions(-DBUILD_DLL) POWERAPI double power(double number) noexcept # elif (defined(_MSC_VER) || defined(_MINGW32_)) # if (defined(_MSC_VER) || defined(_MINGW32_)) & defined(BUILD_DLL) #if defined(_WIN32_) & !defined(_CYGWIN_) Your header "power.h" should be modified: #ifndef POWER_H Solution 2ĭynamic linking in Windows requires that externally visible symbols are declared with the keyword _declspec. Note that this approach is not viable if the power library is not itself being built with CMake, so in that case you will have to stick with the first option. In the second case, have the CMake script that builds the power library perform an install step during which a config file package will get generated, which can then be consumed by your test project. Note that getting this right in a portable way can be very tricky and goes far beyond the scope of a StackOverflow question. In the first case, create a FindPower.cmake script file, make sure it's location is part of the CMAKE_MODULE_PATH and write the code for finding the library and header files and constructing the imported target in there. You can either construct such an imported target manually in the find script, or have CMake do it for you. Exposure of the public include directories for both power.h and power_export.h via that imported target.Association of the library name of the import library (the power.lib file) via that imported target.Providing of an imported target power for consumption by the target_link_libraries call.That call is now responsible for providing all the information that was previously handled by the lines for building the library: With all the magic here happening in the find_package call. If you want to build the dll externally (which is not strictly necessary, but since that's what your question is about.), we have to modify the CMake file to something like: cmake_minimum_required(VERSION 3.5) Note that generare_export_header allows you to customize the generated export header extensively.īe sure you get the project to build and run from this baseline. POWER_EXPORT double power(double number) noexcept To make sure the function gets properly exported, change your header as follows: #ifndef POWER_H Since generated files go to the binary directory tree, we have to adjust the include directories for the library accordingly. Note the use of the generate_export_header function, which instructs CMake to generate macros for exporting functions on shared library interfaces in a portable way. Target_link_libraries(test PRIVATE power) Target_include_directories(power PUBLIC $) Your modelling of the dynamic library is incorrect, both on CMake and on the source level.Īs a starting point, try building the dll as part of the same CMake project as the consuming executable: cmake_minimum_required(VERSION 3.5)Īdd_library(power SHARED power_sources.cpp power.h)
0 Comments
Leave a Reply. |