Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMAKE shared library only creates the DLL and not the associated LIB

Tags:

cmake

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

like image 655
Luca Avatar asked Oct 31 '25 07:10

Luca


1 Answers

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_SYMBOLS

Enable this boolean property to automatically create a module definition (.def) file with all global symbols found in the input .obj files for a SHARED library (or executable with ENABLE_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 explicit dllexport markup, even in C++ classes.

...

This property is initialized by the value of the CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS variable if it is set when a target is created.

like image 120
Adayah Avatar answered Nov 04 '25 18:11

Adayah