简体   繁体   中英

Error in target_link_libraries when linking GSL in CLion CMakeLists.xt

I'm trying to use a C++ code in CLion which relies on the external C library GSL.

I created a CMakeLists.txt file as:

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)

but when I compile it I get the follwing error:

[ 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

Any suggestion on what could cause this?

The problem in my code in CMakeLists.txt was simple and trivial, I just did not see it.

In fact, had to remove the 'main.cpp' declaration within target_link_libraries.

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)

This code worked.

result: 5.05537 0.401813 0.150471
error : 0.0553737 0.00181308 0.000470529

Process finished with exit code 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM