Reputation: 57
I can't figure out how to link my static library with my test binaries using cmake on Windows. What am I doing wrong?
The structure of the project is like below
MyProject
- build
- include
- catch
- *.h
- MyProject
- *.h
- src
- *.c
- test
- *.cpp
- CMakeLists.txt
And the CMakeLists.txt
CMAKE_MINIMUM_REQUIRED ( VERSION 3.3.1 )
SET ( NAME_LIB "myproject" )
SET ( NAME_TEST "test_myproject" )
SET ( PATH_BUILD "${PROJECT_SOURCE_DIR}/build" )
SET ( PATH_INCLUDE "${PROJECT_SOURCE_DIR}/include" )
SET ( PATH_SOURCE "${PROJECT_SOURCE_DIR}/src" )
SET ( PATH_TEST "${PROJECT_SOURCE_DIR}/test" )
FILE ( GLOB SOURCES_LIB "${PATH_SOURCE}/*.c" "${PATH_SOURCE}/*.cpp" )
FILE ( GLOB SOURCES_TEST "${PATH_TEST}/*.c" "${PATH_TEST}/*.cpp" )
INCLUDE_DIRECTORIES ("${PATH_INCLUDE}")
ADD_LIBRARY( ${NAME_LIB} STATIC "${SOURCES_LIB}" )
ADD_EXECUTABLE ( ${NAME_TEST} "${SOURCES_TEST}" )
TARGET_LINK_LIBRARIES ( ${NAME_TEST} "${PATH_BUILD}/${NAME_LIB}" )
Creating the visual studio project using cmake goes okay, but when I try to build the solution using msbuild I get the following error.
(Link target) ->
LINK : fatal error LNK1104: cannot open file 'myproject.obj' [D:\Source\myproject\build\test_myproject.vcxproj]
Error when specifying the library name instead of the absolute path.
test_myproject.obj : error LNK2019: unresolved external symbol "int __cdecl myproject_parse(char const *)" (?myproject_parse@@YAHPBD@Z
) referenced in function _main [D:\Source\myproject\build\test_myproject.vcxproj]
D:\Source\myproject\build\Debug\test_myproject.exe : fatal error LNK1120: 1 unresolved externals [D:\Source\myproject\build\test_
myproject.vcxproj]
Upvotes: 2
Views: 1892
Reputation: 452
try linking your library by target name, also maybe make sure the sources for the lib contain the files you expect, the other possibility is that no symbols are compiled
TARGET_LINK_LIBRARIES ( ${NAME_TEST} ${NAME_LIB} )
Upvotes: 2