Link errors when building shared library even though source files are included - dll

I am specifying a shared library build from source files in my CMakeLists.txt file like so:
# Library setup
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Source/*.cpp)
file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/Source/*.h)
Inside the source directories contain all my .h and .cpp files required to build my shared library. So I then do something like this:
add_library(mylibrary SHARED ${SOURCES} ${HEADERS} )
I also link a bunch of other .libs with mylibrary later on as well (Could there be a clashing issue?). The problem arises when I try to build mylibrary. I receive linking errors such as:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol __imp_cosf mylibrary ***.obj 1
even though the symbol is defined in my source files that I have included. I am not exactly sure what to do in order to properly let my project find those symbols. The funny thing is is that when I build as a static library it is fine. However, when I try to build as a dynamic library, these errors appear.

So, I'm not exactly sure why this worked. But there was a conflict warning between linking to MSVRCT and libcmt. It said something like this in the output log:
Resolving LNK4098: defaultlib 'MSVCRT' conflicts with ...
This is because libcmt is a static library for release build and uses \MT flags. MSVRCT is a release DLL version and compiled with \MD flag. Because of this conflict, I had to change my compiler flags to be:
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
Then the symbols were able to be found. If anyone has any comments/corrections to why this worked please add.

Related

cmake with vcpkg can't find *.lib files

I want to use DirectXTK via vcpkg and link it with my cmake project.
vcpkg.exe list shows the following:
directxtk:x86-windows apr2021 A collection of helper classes for writing Direc...
directxtk:x86-windows-static-md apr2021 A collection of helper classes for writing Direc...
libzip:x86-windows 1.7.3#2 A library for reading, creating, and modifying z...
libzip:x86-windows-static-md 1.7.3#2 A library for reading, creating, and modifying z...
zlib:x86-windows 1.2.11#10 A compression library
zlib:x86-windows-static-md 1.2.11#10 A compression library
[dependnencies from these are not included here]
You can see that DirectXTK,libzip and zlib is installed.
Now my CMakeLists.txt looks like this:
find_package(ZLIB REQUIRED)
target_link_libraries(Game PRIVATE ZLIB::ZLIB)
if(WIN32)
find_package(directxtk CONFIG REQUIRED)
target_link_libraries(Game PRIVATE DirectXTK)
endif()
find_package(libzip REQUIRED)
target_link_libraries(Game PRIVATE libzip)
When I try to compile my project, it says
fatal error LNK1104: cannot open file 'DirectXTK.lib'
fatal error LNK1104: cannot open file 'libzip.lib'
It can't find the lib files for DirectXTK and libzip when linking.
It compiles fine though, which means it can find zlib.h and DirectXTK/WICTextureLoader for example.
How am I supposed to fix that?
The cmake Toolchain is correctly set to vcpkg.cmake and I used vcpkg.exe integrate install prior.

CMake importing both shared and static library versions, but I only want one

I would like to use the Antlr framework in a project. I'm using CMake to build the project.
I would like to use the SHARED library version of Antlr, not the STATIC one. Its CMake file contains targets for both.
Antlr's github site explicity tells me to use the following code:
find_package(antlr4-runtime REQUIRED)
# add runtime include directories on this project.
include_directories( ${ANTLR4_INCLUDE_DIR} )
# add runtime to project dependencies
add_dependencies( Parsertest antlr4_shared )
# add runtime to project link libraries
target_link_libraries( Parsertest PRIVATE
antlr4_shared)
(another target, antlr4_static, exists, but shouldn´t be used.)
I copied it exactly like this and am getting the following error:
CMake Error at /usr/lib64/cmake/antlr4-runtime/antlr4-targets.cmake:82 (message):
The imported target "antlr4_static" references the file
"/usr/lib/libantlr4-runtime.a"
but this file does not exist.
I dont have the static library installed in my system as I have no intention of using it. Still, how do I make CMake stop looking for the wrong target in the first place? I use it nowhere in my CMakeLists.txt file and am puzzled by this behavior.

CMake does not generate rules for IMPORTED_IMPLIB

I recently started fixing some broken CMake-based build.
The project relies on a pre-built library (think .lib & .dll file).
The dependency is treated as an imported target; i.e. like this:
add_library(the_dependency SHARED IMPORTED GLOBAL)
set_target_properties(
the_dependency
PROPERTIES
IMPORTED_IMPLIB "definitely_correct_path/the_dependency.lib"
)
Building the project using the Visual Studio Generator works fine. However, using Ninja breaks the build leaving me with this error:
ninja: error: 'the_dependency.lib', needed by 'awesome/target', missing and no known rule to make it
Pretty much the same goes for NMake:
NMAKE : fatal error U1073: don't know how to make 'the_dependency.lib'
What I tried:
I defined the IMPORTED_LOCATION property additionally.
I checked the paths and they're definitely correct.
Also, peaking into the generated build files (i.e. build.ninja), it provides a rule for "generating" the DLL, but none for the LIB file.
Is there something I am missing?
Have a look at
-DENABLE_EXPORTS=TRUE
https://cmake.org/cmake/help/latest/prop_tgt/ENABLE_EXPORTS.html#prop_tgt:ENABLE_EXPORTS

Explicitly tell to CMake target_link_libraries() to read dependency as target and not as a file from system path?

I got multiple warnings:
CMake Warning at libsysev/src/CMakeLists.txt:17 (add_library):
Cannot generate a safe runtime search path for target sysev because files
in some directories may conflict with libraries in implicit directories:
runtime library [libzlog.so] in /usr/lib/arm-linux-gnueabihf may be hidden by files in:
/home/user/project/BUILD/libzlog/src
Some of these libraries may not be found correctly.
Target defined as following:
add_library(sysev SHARED main.cpp)
target_link_libraries(sysev PUBLIC zlog)
zlog provided in source code and also installed to the system.
Root CMakeLists.txt looks like:
project(xxx)
add_subdirectory(zlog)
add_subdirectory(sysev)
I want to disambiguate zlog. CMake must know this is a target and not a file from system path.

CMake library file missing

I'm currently struggling with cmake.
I'm using Cmake for an embedded platform with GCC.
My project is separate into several modules. Each module is build into a static library. At link time, all of these libraries are collected and linked into one binary.
The problem: I created a new folder for some unit tests. All sources are build into a library libunit_tests.a.(I checked the library actually gets created).
However in my linker call other libraries are passed to the linker, mine however gets omitted resulting in an undefined reference error.
My folder structure looks like this
*
unit_tests/
*
unit_tests/inc
*unit_tests/src
There is one Cmake file located at
- /unit_tests/CMakeLists.txt
My actual CMakeLists.txt file is pretty basic
include_directories("./inc")
set(module_name "unit_tests")
set(MODULE_SOURCES
./inc/active_tests.h
./inc/Run_All_Tests.h ./src/Run_All_Tests.c
)
###########################
# add library
###########################
if(MODULE_SOURCES)
# add files to library
add_library("${module_name}"
${MODULE_SOURCES})
target_link_libraries("${module_name}"
-Wl,--start-group
-Wl,--end-group)
endif()
How do i pass this library to the linker to resolve the undefined reference error?
I thought this is done via add_libary and target_link_libraries?