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})
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
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
Edit: Im using MinGW-x86_64 on windows
When Im trying to compile a gtk+ 3 c project with cmake, I have this error message on the log:
"C:\Program Files\JetBrains\CLion 2017.1.3\bin\cmake\bin\cmake.exe" --build C:\Users\Jonas\ClionProjects\tutorial\cmake-build-debug --target tutorial -- -j 4
[ 50%] Linking C executable tutorial.exe
c:/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../../mingw32/bin/ld.exe: cannot find -ldwmapi
collect2.exe: error: ld returned 1 exit status
CMakeFiles\tutorial.dir\build.make:96: recipe for target 'tutorial.exe' failed
mingw32-make.exe[3]: *** [tutorial.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/tutorial.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/tutorial.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/tutorial.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/tutorial.dir/rule] Error 2
mingw32-make.exe: *** [tutorial] Error 2
Makefile:117: recipe for target 'tutorial' failed
This is my CMakeLists.txt, in there I added gtk3 and its libraries, but when Im trying to compile, it has the errors mentioned before.
cmake_minimum_required(VERSION 3.7)
project(tutorial)
set(CMAKE_C_STANDARD 99)
set(SOURCE_FILES main.c)
set(PKG_CONFIG_EXECUTABLE "C:/msys64/mingw64/bin/pkg-config.exe")
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0)
INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK3_LIBRARY_DIRS})
add_executable(tutorial ${SOURCE_FILES})
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER})
TARGET_LINK_LIBRARIES(tutorial ${GTK3_LIBRARIES})
dwmapi.dll is provided only for Vista. So I guess that it is a bug of package of gtk+-3.0. And -ldwmapi is not required for your OS. Below is a workaround to fix this issue. I don't make sure this will solve your issue. Note that this is self-responsibility.
make backup-copy of gdk-3.0.pc, gdk-broadway-3.0.pc, gdk-win32-3.0.pc in C:\msys64\mingw64\lib\pkgconfig
open those files in vim, remove -ldwmapi and :wq
UPDATE
Let's create libdwmapi.a
download def file from here
dlltool -d dwmapi.def -l libdwmapi.a
I'm trying to use a simple shared library that I made with a file that just contains a main method.
I first ran cmake . which worked fine and didn't return any errors.
Then I ran make but got this error:
$ make
Scanning dependencies of target myprog
[ 50%] Building C object CMakeFiles/myprog.dir/main.c.o
[100%] Linking C executable myprog.exe
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lhello-user
collect2: error: ld returned 1 exit status
clang-3.8: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/myprog.dir/build.make:95: myprog.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/myprog.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
The CMakeLists.txt file
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
set(CMAKE_C_COMPILER clang)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)
The library exists inside of /usr/local/lib/ as libhello-user.dll.a
Note: Im using Cygwin for cmake and make
Turning my comment into an answer
See CMake/Tutorials/Exporting and Importing Targets.
You either have:
to name a full path for the library
CMake is not searching for it automatically
you would have to add something like find_library(_lib_path NAMES hello-user)
or - better - put those into an IMPORTED target
cmake_minimum_required(VERSION 2.8.8)
project(LIB_EXAMPLE)
add_library(hello-user SHARED IMPORTED GLOBAL)
set_target_properties(
hello-user
PROPERTIES
IMPORTED_LOCATION /usr/local/lib/libhello-user.dll
IMPORTED_IMPLIB /usr/local/lib/libhello-user.dll.a
)
add_executable(myprog main.c)
target_link_libraries(myprog hello-user)
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)