在CMakeLists.txt中链接GSL [英] Linking GSL in CMakeLists.txt

查看:96
本文介绍了在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:

这篇关于在CMakeLists.txt中链接GSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆