I am using CMAKE for configuring my project for Visual Studio and I have the following setup:
PROJECT(Proj1)
CMAKE_MINIMUM_REQUIRED(VERSION 3.2.0)
# RPATH stuff - to avoid losing linking information
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Variable for header and source files
SET(HEADERS
api/mylib.h
)
SET(SOURCES
api/mylib.cpp
)
# Compile and link
ADD_LIBRARY(${NAME} SHARED ${HEADERS} ${SOURCES})
TARGET_LINK_LIBRARIES(${NAME} ${LIBS})
Now, this creates the project and I can compile it but it only produces the Proj1.dll file and not the associatwed lib file. I thought both the library and the shared object file should have been produced.
I am using CMAKE 3.11.0 and Visual Studio Community 2017
If you put
set( CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS True )
at the front of the top-level CMakeLists file, all symbols* from all shared libraries built within the project will be automatically exported, thus producing the relevant .lib files. This saves you from exporting each symbol manually via __declspec(dllexport).
You can also set the WINDOWS_EXPORT_ALL_SYMBOLS property individually for selected shared library targets so that only the libs of your choice export their symbols automatically.
*All symbols except the global data symbols, for which I couldn't find an easy fix.
From CMake docs:
WINDOWS_EXPORT_ALL_SYMBOLSEnable this boolean property to automatically create a module definition (
.def) file with all global symbols found in the input.objfiles for aSHAREDlibrary (or executable withENABLE_EXPORTS) on Windows. The module definition file will be passed to the linker causing all symbols to be exported from the.dll. For global data symbols,__declspec(dllimport)must still be used when compiling against the code in the.dll. All other function symbols will be automatically exported and imported by callers. This simplifies porting projects to Windows by reducing the need for explicitdllexportmarkup, even inC++classes.
...This property is initialized by the value of the
CMAKE_WINDOWS_EXPORT_ALL_SYMBOLSvariable if it is set when a target is created.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With