1

我正在尝试在依赖于外部 C 库 GSL 的 CLion 中使用 C++ 代码。

我创建了一个 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.20)
project(untitled)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_COMPILER /usr/local/Cellar/gcc/11.1.0_1/bin/g++-11)
set(CMAKE_CXX_FLAGS " -std=c++17 -mpopcnt -L/usr/local/lib")

include_directories(${PROJECT_BINARY_DIR}/Include /usr/local/include)

add_executable(untitled main.cpp)
find_package(GSL REQUIRED)
include_directories(${GSL_INCLUDE_DIR})
target_link_libraries(untitled main.cpp GSL::gsl)

但是当我编译它时,我得到了以下错误:

[ 50%] Linking CXX executable untitled
ld: library not found for -lmain.cpp
collect2: error: ld returned 1 exit status
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2

关于可能导致这种情况的任何建议?

4

1 回答 1

0

我在 CMakeLists.txt 中的代码中的问题很简单,我只是没看到。

事实上,必须删除 target_link_libraries 中的 'main.cpp' 声明。

cmake_minimum_required(VERSION 3.20)
project(untitled)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_executable(untitled main.cpp)
find_package(GSL REQUIRED)
include_directories(${GSL_INCLUDE_DIR})
target_link_libraries(untitled GSL::gsl)

该代码有效。

result: 5.05537 0.401813 0.150471
error : 0.0553737 0.00181308 0.000470529

Process finished with exit code 0
于 2021-09-26T12:01:07.110 回答