在我的 Cmake 项目中使用 Eigen Lib?

Using Eigen Lib in my Cmake project?(在我的 Cmake 项目中使用 Eigen Lib?)

本文介绍了在我的 Cmake 项目中使用 Eigen Lib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 CMake 项目中使用仅标头库 (Eigen) 时遇到困难.当我删除与 Eigen 库相关的所有部分时,它会编译,但不确定如何使用 (Eigen) 进行构建.请注意,Eigen 在 Eigen 文件夹中有一个 CmakeLists.txt,它有/src 文件夹,其中包含与矩阵运算等相关的(*.h 和 *.cpp)...

I am having difficulty using a header-only library (Eigen) in my CMake project. When i take off all the portion related to Eigen library it compiles, but not sure how to build with (Eigen). Note that Eigen has a CmakeLists.txt in Eigen folder, and it has /src folder having (*.h and *.cpp) related to Matrix operation etc...

我的程序结构如下

Myproject(文件夹)由:

Myproject (folder) is composed of :

  • CmakeLists.txt
  • /构建
  • /来源

Source 文件夹中有一堆我的文件(*.h 和 *.cpp)和/Eigen(文件夹).

The Source folder has bunch of my files (*.h and *.cpp) and the /Eigen (folder).

我所做的是:

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 )

When i cd to /Build and type (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 ,内容如下:

The eigen folder has the CMakeLists.txt with the following content :

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)

推荐答案

您只需要 INCLUDE_DIRECTORIES 中的正确路径(还要确保包含正确的文件夹或子文件夹,具体取决于您在 C++ 文件中是否使用 #include Eigen/something.h 或 #include something.h )因此,删除行 ADD_LIBRARY(Eigen ... 和 TARGET_LINK_LIBRARIES Eigen.

You just need the correct path in INCLUDE_DIRECTORIES (also make sure to include the correct folder or subfolder, depending if in your c++ file you are using #include Eigen/something.h or #include something.h ) So, remove the lines ADD_LIBRARY(Eigen ... and TARGET_LINK_LIBRARIES Eigen.

为了排除故障,您还可以包含 Eigen 文件夹的绝对路径,然后当您运行时,将其转换为相对路径

For troubleshooting, you can also include the absolute path of the Eigen folder , and then when you get it working, transform it in a relative path

这篇关于在我的 Cmake 项目中使用 Eigen Lib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在我的 Cmake 项目中使用 Eigen Lib?

基础教程推荐