I'm currently learning CMake and I'm trying to create my first test project. I'm able to get a simple project up and running in visual studio via CMake. However, I'm having trouble trying to add a library. I've read some guides and things but I keep getting errors. Basically, I'm trying to link SDL libraries (a game programming library) in my sample project. I've placed these libraries in a top level, 'ThirdParty' folder. Here is what my CmakeLists.txt file looks like in my top level directory:
cmake_minimum_required(VERSION 2.8.11)
project(Hello)
#Find necessary header files
find_path(SDL_INCLUDE_DIR SDL.h HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/include/)
#Find necessary library files
find_library(SDL_LIB_DIR SDL2 HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)
find_library(SDLMAIN_LIB_DIR SDLmain HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)
#Add/Link files to project
include_directories(${SDL_INCLUDE_DIR})
target_link_libraries(Test PUBLIC ${SDL_LIB_DIR})
target_link_libraries(Test PUBLIC ${SDLMAIN_LIB_DIR})
add_executable(Test "${CMAKE_SOURCE_DIR}/Source/Main.cpp")
I'm not 100 percent sure of the HINTS parameter, but I saw it used on another thread. Anyway, here's the error I keep getting:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
SDLMAIN_LIB_DIR
linked by target "Test" in directory C:/Users/Jason/Desktop/Test
What am I doing wrong and how do I properly link libraries in CMake?
find_library
or find_package
, and then cmake will set some variables that you can use to link to/use the library. I'm not familiar with SDL, but by googling a little bit about it, I would say this is how it should look like:
find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)
add_executable(MyExec main.cpp)
target_include_directories(MyExec ${SDL2_INCLUDE_DIR})
target_link_libraries(MyExec ${SDL2_LIBRARY})
That find_library
will set the variables SDL2_INCLUDE_DIR
and SDL2_LIBRARY
, which you can use to link to SDL and add its includes to your project.
I'm trying to do the same, but when I try to use my bookstore it doesn't recognize it.
catkin_package(
INCLUDE_DIRS include ${EXTERNAL_INCLUDE_DIRS}
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS message_runtime roscpp actionlib
DEPENDS
)
## Setup include dirs
include_directories(include ${catkin_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIRS})
include_directories(
SYSTEM
${EXTERNAL_INCLUDE_DIRS}
${catkin_INCLUDE_DIRS}
)
# Trajectory planners
add_library(${PROJECT_NAME}_planners
src/PRM_planner.cpp
)
target_link_libraries(${PROJECT_NAME}_planners
${catkin_LIBRARIES}
${RL_LIBRARIES}
)
add_executable(trajectory_planner src/trajectory_planner.cpp)
target_link_libraries(trajectory_planner
${PROJECT_NAME}_planners
${catkin_LIBRARIES}
${RL_LIBRARIES}
)
In my file trajectory_planner.cpp:
#include "trajectory/prm_planner.h"
int main()
{
prm_planner::plan();
return 0;
}
But I have this error:
CMakeFiles/trajectory_planner.dir/src/trajectory_planner.cpp.o: In function `main':
trajectory_planner.cpp:(.text+0x5): undefined reference to `prm_planner::plan()'
collect2: error: ld returned 1 exit status
toolbox_ur3/CMakeFiles/trajectory_planner.dir/build.make:391: recipe for target '/home/euroage/catkin_ws/devel/lib/toolbox_ur3/trajectory_planner' failed
make[2]: *** [/home/euroage/catkin_ws/devel/lib/toolbox_ur3/trajectory_planner] Error 1
CMakeFiles/Makefile2:15231: recipe for target 'toolbox_ur3/CMakeFiles/trajectory_planner.dir/all' failed
make[1]: *** [toolbox_ur3/CMakeFiles/trajectory_planner.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.