Reputation: 4763
I have a cmake setup that is supposed to first build a library and then use this library to create an executable:
# build and install the project lib
add_library(lib${PROJECT_NAME}
test.cpp
)
install(TARGETS lib${PROJECT_NAME} DESTINATION ${INSTALL_DIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${INSTALL_INCLUDE_DIR} FILES_MATCHING PATTERN "*.h*")
# link lib and create executable
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(lib${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_DIR})
The lib building part works, but it seems the linking of the lib does not. I am using a simple add method which is implemented in the test.cpp and I get an undefined reference to this method.
What am I missing here?
Upvotes: 0
Views: 108
Reputation: 6714
The first argument to a target_link_libraries
call has to be the name of the target the library should be linked to, i.e. ${PROJECT_NAME}
in your case, the second argument is the library target. See documentation for target_link_libraries.
In your example you need to use: target_link_libraries(${PROJECT_NAME} lib${PROJECT_NAME})
Upvotes: 1