气哭了的C++调试,cmake 找不到 eigen

这才刚刚开头,可是就是不知道错误在哪里?百度了问题后,打开了很多很多相关的解答,从昨天上午遇到这个问题,历经昨天下午和晚上,还是错误,终于在今天上午圆满解决了问题,但是也为自己的低效率担忧。

错误截图如下:

CMake Error atCMakeLists.txt:23 (find_package):

By not providing"FindEigen3.cmake" in CMAKE_MODULE_PATH this project has

asked CMake tofind a package configuration file provided by "Eigen3", but

CMake did not findone.


Could not find apackage configuration file provided by "Eigen3" with any

of the followingnames:


Eigen3Config.cmake

eigen3-config.cmake


Add theinstallation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set

"Eigen3_DIR"to a directory containing one of the above files. If "Eigen3"

provides aseparate development package or SDK, be sure it has been

installed.


百度的原因都是说项目下的 CMakeLists.txt 配置的不正确,无论如何配置都不正确

不过还好还好,终于找到终极解决方法。

以root 账号进入 Fedora 系统, 普通用户执行的时候出错。

给出项目的大致框架:项目名: TestDemo  ,  eigen 在文件夹 TestDemo中,

运行语句: cmake .. -DEIGEN3_INCLUDE_DIR=eigen -DCMAKE_CXX_COMPILER=/usr/bin/clang++

出现开头的错误。

解决方法如下, 原地址: http://stackoverflow.com/questions/34138879/unable-to-find-eigen3-with-cmake

  • Go to the Eigen source directory and run the CMake and installation steps

    > mkdir build
    > cd build
    > cmake ..
    > make install
问题完美解决。执行结果如下:

[root@localhost build]# cmake .. -DEIGEN3_INCLUDE_DIR=eigen -DCMAKE_CXX_COMPILER=/usr/bin/clang++
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   program_options
--   serialization
-- Configuring done
-- Generating done
-- Build files have been written to: /root/software/LSTM-ER/build

错误原因分析:下载的Eigen 是源码(个人认为,待考证)


 


### 使用Eigen库的CMake配置 为了在C++项目中集成并使用Eigen库,可以按照如下方法设置项目的`CMakeLists.txt`文件: #### 寻Eigen包 通过`find_package()`函数来定位系统中的Eigen库。如果已经安装了Eigen,则该命令会自动到它。 ```cmake find_package(Eigen3 REQUIRED) include_directories(${EIGEN3_INCLUDE_DIR}) ``` 这段代码确保编译器能够访问Eigen头文件[^1]。 #### 定义可执行文件及其依赖关系 创建一个简单的源码文件(例如`main.cpp`),其中包含了对Eigen功能调用的例子;接着定义对应的可执行程序,并指定其所需的外部库。 ```cpp // main.cpp #include <iostream> #include <Eigen/Dense> int main(){ Eigen::MatrixXd m(2, 2); m << 1, 2, 3, 4; std::cout << "Here is the matrix m:\n" << m << std::endl; } ``` ```cmake add_executable(my_program main.cpp) target_link_libraries(my_program PRIVATE Eigen3::Eigen) ``` 上述指令告诉CMake将`my_program`与Eigen静态链接在一起[^2]。 #### 设置构建选项 当希望调整优化级别或者启用调试模式时,可以通过修改CMake缓存变量来进行控制。比如切换至Release版本以获得更好的性能表现: ```bash $ mkdir -p build && cd build $ cmake .. -DCMAKE_BUILD_TYPE=Release ``` 此操作会在后续编译过程中应用更高的优化等级[^3]。 #### 验证环境准备情况 确认所使用的工具链是否支持所需特性也很重要。下面展示了如何验证加载的C/C++编译器信息: ```cmake message("-- Is the C++ compiler loaded? ${CMAKE_CXX_COMPILER_LOADED}") message("-- The C++ compiler ID is: ${CMAKE_CXX_COMPILER_ID}") if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU") message("-- Is the C++ from GNU?") endif() message("-- The C++ compiler version is: ${CMAKE_CXX_COMPILER_VERSION}") message("-- Is the C compiler loaded? ${CMAKE_C_COMPILER_LOADED}") message("-- The C compiler ID is: ${CMAKE_C_COMPILER_ID}") if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU") message("-- Is the C from GNU?") endif() message("-- The C compiler version is: ${CMAKE_C_COMPILER_VERSION}") ``` 这些消息可以帮助开发者快速判断当前工作环境中是否存在潜在兼容性问题[^4]。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值