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)
Related
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})
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 am working on build a c library for android 5.1.1.
First I build a standalone toolchain by following the google ndk standalone toolchain guild.
<prebuilt_ndk_r12b_path>/build/tools/make-standalone-toolchain.sh --platform=android-22 --ndk-dir=<prebuilt_ndk_r12b_path> --install-dir=/home/r0ng/utilities/ndk --toolchain=x86_64-linux-android-4.9
After that, I exported CC, AR and RANLIB in ~/.bashrc
export ANDROID_NDK=/home/r0ng/utilities/ndk
SYSROOT=$ANDROID_NDK/sysroot
export CC="$ANDROID_NDK/bin/arm-linux-androideabi-gcc-4.9.x --sysroot=$SYSROOT"
export AR="$ANDROID_NDK/bin/arm-linux-androideabi-gcc-ar --sysroot=$SYSROOT"
export RANLIB="$ANDROID_NDK/bin/arm-linux-androideabi-gcc-ranlib --sysroot=$SYSROOT"
But when I tried to compile by using cmake ... I had the following errors:
-- The C compiler identification is GNU 4.9.0
-- The CXX compiler identification is GNU 4.9.3
-- Check for working C compiler: /home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc
-- Check for working C compiler: /home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc -- broken
CMake Error at /usr/share/cmake-3.5/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "/home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc" is
not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_8d7ca/fast"
/usr/bin/make -f CMakeFiles/cmTC_8d7ca.dir/build.make
CMakeFiles/cmTC_8d7ca.dir/build
make[1]: Entering directory
'/home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_8d7ca.dir/testCCompiler.c.o
/home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc
--sysroot=/home/r0ng/utilities/ndk -o
CMakeFiles/cmTC_8d7ca.dir/testCCompiler.c.o -c
/home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_8d7ca
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_8d7ca.dir/link.txt
--verbose=1
/home/r0ng/utilities/ndk/bin/arm-linux-androideabi-gcc
--sysroot=/home/r0ng/utilities/ndk
CMakeFiles/cmTC_8d7ca.dir/testCCompiler.c.o -o cmTC_8d7ca -rdynamic
/home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
error: cannot open crtbegin_dynamic.o: No such file or directory
/home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
error: cannot open crtend_android.o: No such file or directory
/home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
error: cannot find -lc
/home/r0ng/utilities/ndk/bin/../lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld:
error: cannot find -ldl
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_8d7ca.dir/build.make:97: recipe for target 'cmTC_8d7ca'
failed
make[1]: *** [cmTC_8d7ca] Error 1
make[1]: Leaving directory
'/home/r0ng/projects/relic/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_8d7ca/fast' failed
make: *** [cmTC_8d7ca/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:6 (project)
-- Configuring incomplete, errors occurred!
See also "/home/r0ng/projects/relic/build/CMakeFiles/CMakeOutput.log".
See also "/home/r0ng/projects/relic/build/CMakeFiles/CMakeError.log".
It seems that the compiler is unable to find "crtbegin_dynamic.o", "crtend_android.o", "libc" neither "libdl". But when I checked, those files are all in the folder $HOME/utilities/ndk/sysroot/usr/lib. And I already set the --sysroot in CC, AR and RANLIB.
Update 1 :
After changed the toolchain generation command based on Dan Albert's comment, The command cmake .. is able to be run successfully. But when I run make . I got following error:
arm-linux-androideabi-gcc-4.9.x: error: unrecognized command line option '-m64'
src/CMakeFiles/relic.dir/build.make:62: recipe for target 'src/CMakeFiles/relic.dir/relic_err.c.o' failed
make[2]: *** [src/CMakeFiles/relic.dir/relic_err.c.o] Error 1
CMakeFiles/Makefile2:120: recipe for target 'src/CMakeFiles/relic.dir/all' failed
make[1]: *** [src/CMakeFiles/relic.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2
Thanks,
Environment:
OS: Ubuntu 16.04 LTS
gcc / g++: 4.9.3
AOSP: 5.1.1_r30
NDK: android-ndk-r12b
cmake: 3.5.1
If you want to use cmake, you don't need a standalone toolchain. Android Studio natively supports cmake now: http://tools.android.com/tech-docs/external-c-builds
NDK r13 (not released yet) will also ship a cmake toolchain file for using cmake directly (the method shown in https://stackoverflow.com/a/5099229/632035).
You shouldn't add --sysroot= explicitly. Standalone toolchain already knows about proper one.
I wrote a cmake file that builds two shared library a vector3.c and a matrix4.c
The matrix4 depends on the vector3, if I run my cmake project and only build the vector3 shared library it builds just fine, when I add the matrix4 shared library I get a cmake error. No rule to make target.
The folder structure
core_math
------CMakeLists.txt
-common/
------common_structs.h
-matrix4/
------CMakeLists.txt
------matrix4/src/matrix4_scalar.c
------matrix4/headers/matrix4_scalar.h
-vector3/
-------CMakeLists.txt
-------vector3/src/vector3_scalar.c
-------vector3/headers/vector3_scalar.h
core_math/CMakeLists.txt
PROJECT(core_math)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
ADD_SUBDIRECTORY(vector3_scalar)
ADD_SUBDIRECTORY(matrix4_scalar)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY $(CMAKE_BINARY_DIR)/static_lib)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY $(CMAKE_BINARY_DIR)/shared_lib)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY $(CMAKE_BINARY_DIR)/bin)
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
core_math/vector3/CMakeLists.txt
PROJECT(vector3_scalar)
SET(CMAKE_MACOSX_RPATH 1)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../common)
SET(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../common/common_structs.h)
SET(SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/vector3_scalar.c ${HEADER_FILES})
ADD_LIBRARY(vector3_scalar SHARED ${SRC_FILES})
core_math/matrix4_scalar/CMakeLists.txt
PROJECT(matrix4_scalar)
SET(CMAKE_MACOSX_RPATH 1)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../common
${CMAKE_CURRENT_SOURCE_DIR}/../vector3_scalar)
SET(HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/../common/common_structs.h
${CMAKE_CURRENT_SOURCE_DIR}/../vector3_scalar/headers/vector3_scalar.h
${CMAKE_CURRENT_SOURCE_DIR}/headers/matrix4_scalar.h)
SET(SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/matrix4_scalar.c ${HEADER_FILES})
ADD_LIBRARY(matrix4_scalar SHARED ${SRC_FILES})
TARGET_LINK_LIBRARIES(matrix4_scalar vector3_scalar)
build error
-- Build files have been written to: /Users/blubee/MATH/project/build
Scanning dependencies of target vector3_scalar
[ 25%] Building C object source/core_math/vector3_scalar/CMakeFiles/vector3_scalar.dir/sr
c/vector3_scalar.c.o
[ 50%] Linking C shared library $(CMAKE_BINARY_DIR)/lib/libvector3_scalar.dylib
[ 50%] Built target vector3_scalar
Scanning dependencies of target matrix4_scalar
[ 75%] Building C object source/core_math/matrix4_scalar/CMakeFiles/matrix4_scalar.dir/sr
c/matrix4_scalar.c.o
make[2]: *** No rule to make target `source/core_math/vector3_scalar//Users/blubee/MATH/pr
oject/build/lib/libvector3_scalar.dylib', needed by `source/core_math/matrix4_scalar//Use
rs/blubee/MATH/project/build/lib/libmatrix4_scalar.dylib'. Stop.
make[1]: *** [source/core_math/matrix4_scalar/CMakeFiles/matrix4_scalar.dir/all] Error 2
make: *** [all] Error 2
if I remove the target_link_libraries from the matrix4_scalar cmakelists.txt file then I get undefined symbols error instead.
Undefined symbols for architecture x86_64:
"_vec3_deg2rad", referenced from:
_mat4_rotate in matrix4_scalar.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [source/core_math/matrix4_scalar//Users/blubee/MATH/project/build/lib/libmatr
ix4_scalar.dylib] Error 1
make[1]: *** [source/core_math/matrix4_scalar/CMakeFiles/matrix4_scalar.dir/all] Error 2
make: *** [all] Error 2
I have done some testing with your code and could reproduce the problem.
It's related to having used $(...) brackets for variable dereferencing in the SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ...) command. I've changed $(CMAKE_BINARY_DIR) to ${CMAKE_BINARY_DIR} and everything works fine:
> cmake --build .
-- Configuring done
-- Generating done
-- Build files have been written to: build
Scanning dependencies of target vector3_scalar
[ 25%] Building C object vector3_scalar/CMakeFiles/vector3_scalar.dir/src/vector3_scalar.c.o
[ 50%] Linking C shared library ../shared_lib/libvector3_scalar.dylib
[ 50%] Built target vector3_scalar
Scanning dependencies of target matrix4_scalar
[ 75%] Building C object matrix4_scalar/CMakeFiles/matrix4_scalar.dir/src/matrix4_scalar.c.o
[100%] Linking C shared library ../shared_lib/libmatrix4_scalar.dylib
[100%] Built target matrix4_scalar
References
CMake Error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
What's the CMake syntax to set and use variables?
[CMake] Trouble setting LIBRARY_OUTPUT_DIRECTORY
Custom Directory for CMake Library Output
I'm trying to compile a boost tutorial example from http://www.boost.org/doc/libs/1_36_0/doc/html/boost_asio/tutorial/tuttimer1.html.
My CMakeLists.txt looks like the following:
project(boost)
add_executable(timer1 timer1.cpp)
set_target_properties(timer1 PROPERTIES LINK_FLAGS -lboost_system,-lpthread)
trying to build the whole thing with cmake, I get:
/var/www/C++/boost/build$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /var/www/C++/boost/build
Scanning dependencies of target timer1
[100%] Building CXX object CMakeFiles/timer1.dir/timer1.cpp.o
Linking CXX executable timer1
/usr/bin/ld: cannot find -lboost_system,-lpthread
collect2: ld returned 1 exit status
make[2]: *** [timer1] Błąd 1
make[1]: *** [CMakeFiles/timer1.dir/all] Błąd 2
make: *** [all] Błąd 2
But when I run:
g++ timer1.cpp -lboost_system -lpthread -o timer1
manually, everything works fine. Can someone please point me on what do I do wrong?
PS
When I try to use solution described in Turning on linker flags with CMake, I add to cmake the following lines:
set(CMAKE_SHARED_LINKER_FLAGS "-lboost_system,-lpthread")
set(CMAKE_MODULE_LINKER_FLAGS "-lboost_system,-lpthread")
set(CMAKE_EXE_LINKER_FLAGS "-lboost_system,-lpthread")
and I get the same error as above.
I strongly suggest that you use the CMake integrated FindPackage. CMake will find boost and pthreads for you.
Your CMakeLists.txt should look like this:
find_package( Boost COMPONENTS thread system filesystem REQUIRED ) #whatever libs you need
include_directories( ${Boost_INCLUDE_DIRS} )
find_package( Threads )
In the subfolder src:
set( LIBS_TO_LINK
${Boost_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
)
target_link_libraries( myApp
${LIBS_TO_LINK}
)
/usr/bin/ld: cannot find -lboost_system,-lpthread
Here the linker is looking for a library libboost_system,-lpthread.so. It is exceedingly unlikely that such a library exists on any UNIX system.
You probably want:
set(CMAKE_EXE_LINKER_FLAGS "-lboost_system -lpthread")