问题
I'm trying to add an external library to my project using CMake (v 3.10.1)
I want the libs to live in a specific directory because I like to keep it as clean as possible
My project structure looks like that
Project
|
|-- main.cpp
|-- CMakeLists.txt (top lvl)
|
|-- libs/
|
| -- glew-1.13.0
| -- CMakeLists.txt (lib lvl)
Top lvl CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (myproject)
add_executable(myproject main.cpp)
add_subdirectory (libs)
Lib lvl CMakeList.txt
### GLEW ###
include_directories(
glew-1.13.0/include/
)
set(GLEW_SOURCE
glew-1.13.0/src/glew.c
)
set(GLEW_HEADERS
)
add_library( GLEW_1130 STATIC
${GLEW_SOURCE}
${GLEW_INCLUDE}
)
target_link_libraries(GLEW_1130
${OPENGL_LIBRARY}
${EXTRA_LIBS}
)
main.cpp
#include <iostream>
#include <GL/glew.h>
int main() {
std::cout << "Hello World" << std::endl;
return 0;
}
<GL/glew.h> headerfile not found
What am I' missing in my case so that I can use the glew header files?
回答1:
Effect of include_directories
is not global: being performed from libs/CMakeLists.txt
, it doesn't affect on top-level CMakeLists.txt
.
You may "attach" include directories to the library target:
# In libs/CMakeLists.txt
target_include_directories(GLEW_1130 PUBLIC glew-1.13.0/include/)
so futher linking with that library as target:
# In CMakeLists.txt
target_link_libraries(myproject GLEW_1130)
will automatically propagate its include directories to the executable.
回答2:
Don't add_subdirectory
third party projects into your own build system, otherwise it will pollute your configurations. One reasonable approach here is to install GLEW on your system, and use find_package
in order to include it as part of your project:
cmake_minimum_required (VERSION 2.6)
project (myproject)
find_package(glew 1.13.0)
add_executable(myproject main.cpp)
target_link_libraries(myproject GLEW::glew)
来源:https://stackoverflow.com/questions/47994121/cmake-link-glew-header-file