问题
I am having a bit of trouble with CMake. I am trying to link with GLFW and include multiple source files of my own project. (I'm switching from Visual Studio to make my project cross-platform).
GLFW is in the folder deps/glfw-3.1.1 and my source code is in the folder src
Here is my CMakeLists.txt file:
# Tell CMake to use a minimum of version 3.2
cmake_minimum_required(3.2)
project(Sparky)
# TODO: Versions
# Add all of our source code to the project
file(GLOB_RECURSE Sparky_SOURCES "src/*.cpp")
file(GLOB_RECURSE Sparky_HEADERS "src/*.h")
set(Sparky_INCLUDE_DIRS "")
foreach (_headerFile ${Sparky_HEADERS})
get_filename_component(_dir ${_headerFile} path)
list (APPEND Sparky_INCLUDE_DIRS ${_dir})
endforeach()
list(REMOVE_DUPLICATES Sparky_INCLUDE_DIRS)
add_subdirectory(deps/glfw-3.1.1)
include_directories(${Sparky_INCLUDE_DIRS})
include_directories(deps/glfw-3.1.1/include)
add_executable(Sparky ${Sparky_SOURCES}
target_link_libraries(Sparky glfw ${GLFW_LIBRARIES}))
回答1:
It seems that there is at least an iteration with wrong values for variables _headerFile and path. Try to print values of these variables before launching get_filename_component in the foreach loop using the following code.
message(STATUS "_headerFile: ${_headerFile} )
message(STATUS "path: " ${path} )
Sometimes these types of errors can be generated by wrong values of these parameters.
回答2:
It is common to see this error with "multiple inclusions" of *Config.cmake
files (e.g. you run CMake for lib_A
which uses both lib_B
and lib_C
while lib_B
also links to lib_C
).
You can detect this behaviour through CMake messages:
message(STATUS "_headerFile: ${_headerFile} )
message(STATUS "path: " ${path} )
Then the output will be displayed twice or more, probably with different values each time it enters the *Config.cmake
file.
You can easily solve the problem using IF statements (think of the #ifndef in C headers):
IF (MYPKG_FOUND)
# Already in cache, be silent
SET(MYPKG_FIND_QUIETLY TRUE)
ELSE (EdgeFlow_FOUND)
# do your stuff, you're here for the first time!
ENDIF(MYPKG_FOUND)
回答3:
For me this error was caused by putting list into get_filename_component()
.
Example:
set(abc /path/first /path/second)
get_filename_component(abc_name ${abc} NAME)
The error will look like:
get_filename_component unknown component /path/second
来源:https://stackoverflow.com/questions/31900252/cmake-get-filename-component-unknown-component-directory