I've been effortlessly trying to link SDL2 library to my CLion project. I've downloaded the FindSDL2 script, but I'm still getting undefined reference to an SDL function.
Here's my CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${project_SOURCE_DIR}/cmake")
project(SDL_Test C)
set(CMAKE_C_STANDARD 99)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_Test main.c)
target_link_libraries(SDL_Test ${SDL2_LIBRARY})
After trying to invoke SDL_Init(SDL_INIT_VIDEO) function I'm getting an undefined reference error:
[50%] Building C object CMakeFiles/SDL_Test.dir/main.c.o
[100%] Linking C executable SDL_Test
/bin/ld: CMakeFiles/SDL_Test.dir/main.c.o: in function `main':
/home/bartek/CLionProjects/SDL_Test/main.c:5: undefined reference to `SDL_Init'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/SDL_Test.dir/build.make:84: SDL_Test] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/SDL_Test.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/SDL_Test.dir/rule] Error 2
make: *** [Makefile:118: SDL_Test] Error 2
Related
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
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(testlib C)
set(CMAKE_C_STANDARD 99)
include_directories(/usr/local/include/)
find_library(iconv_lib iconv)
add_executable(testlib library.c)
target_link_libraries(testlib libiconv.a)
In this folder /usr/local/include/ are the files:
iconv.h localcharset.h
The CMake reports the error:
====================[ Build | testlib | Debug ]=================================
D:\.CLion2019.2\system\cygwin_cmake\bin\cmake.exe --build /cygdrive/d/project/c/testlib/cmake-build-debug --target testlib -- -j 4
Scanning dependencies of target testlib
[ 50%] Building C object CMakeFiles/testlib.dir/library.c.o
[100%] Linking C executable testlib.exe
/usr/lib/gcc/x86_64-pc-cygwin/7.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -liconv
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/testlib.dir/build.make:84: testlib.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/testlib.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/testlib.dir/rule] Error 2
make: *** [Makefile:118: testlib] Error 2
It seems like you are not using the library located by find_library(), if it is actually found. If the library is not found, you can add search paths to tell CMake where to find this library:
find_library(iconv_lib
NAMES iconv
PATHS /path/containing/your/iconv/lib
)
Finally, use the iconv_lib variable you defined when calling target_link_libraries(), like this:
target_link_libraries(testlib PUBLIC ${iconv_lib})
the cmake file
cmake_minimum_required(VERSION 3.6)
project(producer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -pthread")
add_executable(server wrappers.h wrappers.c server.c request.h request.c)
the error message
`CMakeFiles/server.dir/wrappers.c.o: In function Pthread_create':
/home/user/ClionProjects/producer/wrappers.c:570: undefined reference to `pthread_create'
CMakeFiles/server.dir/wrappers.c.o: In function `Pthread_join':
/home/user/ClionProjects/producer/wrappers.c:645: undefined reference to `pthread_join'
CMakeFiles/server.dir/wrappers.c.o: In function `Pthread_cancel':
/home/user/ClionProjects/producer/wrappers.c:653: undefined reference to `pthread_cancel'
collect2: error: ld returned 1 exit status
make[3]: *** [server] Error 1
make[2]: *** [CMakeFiles/server.dir/all] Error 2
make[1]: *** [CMakeFiles/server.dir/rule] Error 2
make: *** [server] Error 2
wrapper.c includes wrapper.h which includes pthread.h
I'm overlooking something obvious, but I don't know what.
I tried set, add_library, and install commands but never got output with fewer errors than this.
Edit: Here's the fixed file.
cmake_minimum_required(VERSION 3.6)
project(producer)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g -lpthread")
add_library(wrapper wrapper.h wrapper.c)
add_library(cs537 cs537.h cs537.c)
add_library(request request.h request.c)
add_executable(server server.c)
target_link_libraries(server request wrapper -lpthread)
thats a linker error, you need to use -pthread in the linker aswell, either directly:
target_link_libraries(server -lpthread)
or more cross-platform:
set(CMAKE_THREAD_PREFER_PTHREAD ON)
find_package (Threads)
target_link_libraries (server ${CMAKE_THREAD_LIBS_INIT})
I am using CLion code editor.
I have such structure of project:
This is the content of CMakeLists.txt:
cmake_minimum_required(VERSION 3.4)
project(FirstAgent)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.c)
add_executable(FirstAgent ${SOURCE_FILES})
target_link_libraries(FirstAgent simgrid)
But when I run my program in the code editor the error occur:
/usr/bin/ld: cannot open output file FirstAgent: Is a directory
collect2: error: ld returned 1 exit status
make[3]: *** [FirstAgent] Error 1
make[2]: *** [CMakeFiles/FirstAgent.dir/all] Error 2
make[1]: *** [CMakeFiles/FirstAgent.dir/rule] Error 2
make: *** [FirstAgent] Error 2
How can I avoid it?
You can try setting an output directory, so that the binaries are stored elsewhere:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
I am trying to build osg 14.0 on slackware 14.0 from source. I get linker error while creating executable files from osgviewer:
Linking CXX executable ../../bin/osgviewer
../../lib/libosg.so.3.0.1: undefined reference to `std::__detail::_List_node_base::_M_transfer(std::__detail::_List_node_base*, std::__detail::_List_node_base*)'
../../lib/libosg.so.3.0.1: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)'
../../lib/libosg.so.3.0.1: undefined reference to `std::__detail::_List_node_base::_M_unhook()'
../../lib/libosgDB.so.3.0.1: undefined reference to `std::__detail::_List_node_base::swap(std::__detail::_List_node_base&, std::__detail::_List_node_base&)'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/osgviewer] Error 1
make[1]: *** [applications/osgviewer/CMakeFiles/application_osgviewer.dir/all] Error 2
make: *** [all] Error 2
This is ld.so.conf
/usr/local/lib
/usr/i486-slackware-linux/lib
/usr/lib/seamonkey
/usr/lib
Do you know where the problem could be ?
Thank you.