Linking GSL in CMakeLists.txt(在 CMakeLists.txt 中链接 GSL)
问题描述
我有一个包含多个文件的代码,它使用 GSL 库.当我使用命令通过终端编译代码时
I have a code with multiple files, that uses the GSL Library. When I compile the code through the terminal with the command
g++ main.cpp -lm -lgsl -lgslcblas -o Exec
这会编译并给出正确的输出并且没有错误.但是,当我尝试在 CLion 中构建代码时,出现错误
This compiles and gives the correct output and no errors. However, when I try and build the code in CLion I get the error
undefined reference to `gsl_rng_uniform'
我已经通过 CMakeLists.txt 链接了代码中的各种 .cpp 文件,但我认为,我必须使用类似于标志的内容才能链接到 GSL.我的 CMakeLists.txt 文件目前如下(源文件中只包含 .cpp 文件,而不包含 .h 文件):
I have linked the various .cpp files in my code through the CMakeLists.txt, but I think, I have to something similar to the flags to link to GSL. My CMakeLists.txt file is as follows currently (only the .cpp files are included in the source files, not the .h files):
cmake_minimum_required(VERSION 3.7)
project(Unitsv1)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp
transition.cpp
random.cpp)
add_executable(Unitsv1 ${SOURCE_FILES})
我对 C++ 很陌生,似乎在网上找不到任何答案.谢谢
I'm very new to C++, and can't seem to find any answers online. Thanks
推荐答案
您尚未在 GSL 库中进行链接,因此链接器将找不到它提供的任何符号.像这样的事情应该可以让你大部分时间:
You haven't linked in the GSL libraries, so the linker won't find any of the symbols it provides. Something like this should get you most of the way there:
cmake_minimum_required(VERSION 3.7)
project(Unitsv1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES) # See below (1)
set(SOURCE_FILES main.cpp
transition.cpp
random.cpp)
add_executable(Unitsv1 ${SOURCE_FILES})
find_package(GSL REQUIRED) # See below (2)
target_link_libraries(Unitsv1 GSL::gsl GSL::gslcblas)
如果您的代码使用 C++11,那么您需要 (1) 处的行以确保您真正获得 C++11 支持.如果没有 CMAKE_CXX_STANDARD_REQUIRED YES
,CMAKE_CXX_STANDARD
变量仅用作如果可用,则使用它,或者回退到编译器可以提供的最接近的标准".如果您愿意,您可以在此处找到详细的文章很好奇.
If your code uses C++11, then you need the line at (1) to ensure you actually get C++11 support. Without CMAKE_CXX_STANDARD_REQUIRED YES
, the CMAKE_CXX_STANDARD
variable acts only as "Use it if it is available, or fall back to the closest standard the compiler can provide". You can find a detailed write-up here if you're curious.
您的问题更重要的部分是 (2).find_package()
命令查找 GSL 库等,并将它们用作导入目标 GSL::gsl
和 GSL::gslcblas
.然后使用 target_link_libraries()
将可执行文件链接到它们,如图所示.CMake 文档详细解释了 find_package()
方面的工作原理:
The more important part for your question is at (2). The find_package()
command looks for the GSL libraries, etc. and makes them available as import targets GSL::gsl
and GSL::gslcblas
. You then use target_link_libraries()
to link your executable to them as shown. The CMake documentation explains how the find_package()
side of things works in plenty of detail:
- 从这里开始:
find_package()
- GSL 规范:
FindGSL
模块 - 链接:
target_link_libraries()
这篇关于在 CMakeLists.txt 中链接 GSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 CMakeLists.txt 中链接 GSL
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01