我在CMake项目中使用仅标头库(Eigen)时遇到困难.当我摘下所有与Eigen库相关的部分时,它将进行编译,但不确定如何使用(Eigen)进行构建.请注意,Eigen在Eigen文件夹中有一个CmakeLists.txt文件,并且它的/ src文件夹中有(* .h和* .cpp)与Matrix操作等有关.
我程序的结构如下
Myproject(文件夹)由:
> CmakeLists.txt
> /构建
> /来源
Source文件夹中有一堆我的文件(* .h和* .cpp)和/ Eigen(文件夹).
我所做的是:
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0)
LIST(APPEND CMAKE_CXX_FLAGS
"-std=c++0x
-pthread
${CMAKE_CXX_FLAGS}
-g
-Wall -Wextra ")
ADD_LIBRARY(Eigen ${CMAKE_SOURCE_DIR}/Eigen)
TARGET_INCLUDE_DIRECTORIES(Eigen INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:include/Eigen>
)
INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS})
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHERS})
INCLUDE_DIRECTORIES(include)
ADD_LIBRARY(RTT
Def.cpp
Def.h
krnel.cpp
krnel.h
Mesh.cpp
Mesh.h
Mcom.cpp
Mcom.h
timer.h
Identifier.h)
ADD_EXECUTABLE(Rdrtst main.cpp)
TARGET_LINK_LIBRARIES(Rdrtst RTT ${GTK3_LIBRARIES} Eigen)
当我CD到/ Build并键入(Cmake ../Source)
我得到以下内容:
[/../Build]$cmake ../Source
-- Configuring done
CMake Error: Cannot determine link language for target "Eigen".
CMake Error: CMake can not determine linker language for target:Eigen
-- Generating done
-- Build files have been written to: /../../MyProject/Build
eigen文件夹具有CMakeLists.txt,其中包含以下内容:
include(RegexUtils)
test_escape_string_as_regex()
file(GLOB Eigen_directory_files "*")
escape_string_as_regex(ESCAPED_CMAKE_CURRENT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
foreach(f ${Eigen_directory_files})
if(NOT f MATCHES "\\.txt" AND NOT f MATCHES "${ESCAPED_CMAKE_CURRENT_SOURCE_DIR}/[.].+" AND NOT f MATCHES "${ESCAPED_CMAKE_CURRENT_SOURCE_DIR}/src")
list(APPEND Eigen_directory_files_to_install ${f})
endif()
endforeach(f ${Eigen_directory_files})
install(FILES
${Eigen_directory_files_to_install}
DESTINATION ${INCLUDE_INSTALL_DIR}/Eigen COMPONENT Devel
)
add_subdirectory(src)
解决方法:
您正在尝试将Eigen作为编译库包含在内.但是,正如您所说的,Eigen实际上是仅标头的库,不需要编译,只需included.根本不应该有.cpp文件.
删除线
ADD_LIBRARY(Eigen ${CMAKE_SOURCE_DIR}/Eigen)
因为它的意思是static or shared libraries.既然您没有构建本征,就可以删除该行
TARGET_INCLUDE_DIRECTORIES(Eigen ...
Eigen CMakeLists文件实际上只是将Eigen头文件复制到一个include目录中,并且不编译任何内容.有关如何将Eigen与CMake结合使用的示例,请参见this链接.