Reputation: 1061
This is an example, I googled a lot for this today but I cannot find a good way for have this thing done.
For example I followed what's written in this link: http://mirkokiefer.com/blog/2013/03/cmake-by-example/
I have
/Build
/Src
=> main.c
/Lib
=> File.c
=> File.h
I have a CMakeLists.txt in the Src dir
SET(SOURCES
main.c
)
add_subdirectory(Lib)
ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} lib)
and in the Lib dir:
set(LibSrc
File.c
)
set(LibHead
File.h
)
add_library(lib STATIC ${LibSrc} ${LibHead})
target_include_directories(lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
When I compile the project everything compile corretly, but when the gcc links the library with the rest of the project I receive an error like:
main.cpp:(.text+0x10): undefined reference to `test()`
It is driving me crazy... Any hints on where I'm doing it wrong? Thank you
Upvotes: 1
Views: 106
Reputation: 78468
It looks like your target_link_libraries
command is missing the list of dependencies. I guess in your case, you just want to link "lib", so you need to change this to:
target_link_libraries(${PROJECT_NAME} lib)
As an aside, you're missing a $
before {CMAKE_CURRENT_SOURCE_DIR}
in the line
target_include_directories(lib PUBLIC {CMAKE_CURRENT_SOURCE_DIR})
Upvotes: 2