cmake set linker flags for boost - linker

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")

Related

Using cmake with a shared (dynamic) library

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)

Failed to build C code by using generated NDK toolchain

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.

C Linker unable to find a GSL file. Linux

/usr/bin/ld: cannot find -lgsl
/usr/bin/ld: cannot find -lgslcblas
collect2: ld returned 1 exit status
make[2]: *** [examples/slim_learn] Error 1
make[1]: *** [examples/CMakeFiles/slim_learn.dir/all] Error 2
make: *** [all] Error 2
I am getting a linker error while I use the make command and I think this is because the linker isn't able to find a particular GSL file even after I have set the path for LIBRARY_PATH env variable which apparently the C linker consults if unable to find a file. I am stumped as to how to fix this error. I have also set the C_INCLUDE_PATH to my gsl directory on an unrelated (most probably) note. Any help in resolving this error would be much appreciated as I am very new to linux and have no idea how to proceed.
Setting LIBRARY_PATH :
export LIBRARY_PATH=$LIBRARY_PATH:/home/PATH_TO_gls/gsl-1.16/
The make file is really long hence I am hesitant to post it here but this is the relevant part of the makefile: (please let me know if you would still like me to post the entire thing)
src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o.requires:
.PHONY : src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o.requires
src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o.provides: src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o.requires
$(MAKE) -f src/CMakeFiles/SLIM.dir/build.make src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o.provides.build
.PHONY : src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o.provides
src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o.provides.build: src/CMakeFiles/SLIM.dir/slim_fs_learn.c.o
src/CMakeFiles/SLIM.dir/slim_learn.c.o: src/CMakeFiles/SLIM.dir/flags.make
src/CMakeFiles/SLIM.dir/slim_learn.c.o: ../src/slim_learn.c
$(CMAKE_COMMAND) -E cmake_progress_report "/home/nmuralid/Desktop/Nikhil Documents/Yelp_Research/SLIM Recommender/slim-1.0/b$
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Building C object src/CMakeFiles/SLIM.dir/slim_learn.c.o"
cd "/home/nmuralid/Desktop/Nikhil Documents/Yelp_Research/SLIM Recommender/slim-1.0/build/src" && /usr/bin/gcc $(C_DEFINES)$
Thanks in advance!
The solution was to set LIBRARY_PATH environment variable to the paths of the libgsl.so and libgslblas.so files. This was achieved as follows:
export LIBRARY_PATH=$LIBRARY_PATH:/home/path_to_gsl/gsl-1.16/cblas/./.libs/
export LIBRARY_PATH=$LIBRARY_PATH:export LIBRARY_PATH=$LIBRARY_PATH:/home/nmuralid/path_to_gsl/gsl-1.16/.libs/
We set the LIBRARY_PATH env in this case because the C-Linker looks inside these directories in-case it fails to find the files during linking.
gls-version 1.16 used.

cannot find -lpthread

I am trying to cross compile samba server.
./config works just fine, but when I want to build with make the prompt says:
Linking shared library bin/libsmbclient.so.0
/opt/qnx630/host/linux/x86/usr/bin/ntoarm-ld: cannot find -lpthread
collect2: ld returned 1 exit status
make: *** [bin/libsmbclient.so.0] Error 1
Anyone knows a solution?
had this same problem w/mingw. got libpthreadGC2.a from ftp://sourceware.org/pub/pthreads-win32/dll-latest/lib/x86/, put it in C;\mingw\lib and renamed it to libpthread.a. i didn't know gcc's -l switch means "apprend 'lib' to the front and '.a' to the back" e.g. -lpthread = libpthread.a
Make sure that the pthread library is in the library search path of the linker.

Two basic questions on the -l(blah) flag while compiling / installing programs

I am new to Ubuntu and I had the following questions.
When compiling a C or any other program one often writes a command such as gcc hello.c -lm
I am told the -lm option is for explicitly linking the math library during the compiling
phase. I wanted to know where the library 'foo' is supposed to be located if linked with - lfoo.
This is an error I get when I was trying to install a package named SUPERLU which in turn depends on the existence of a package named PARMETIS. (ending of the stuff after running make)
/usr/bin/ld: cannot find -lparmetis
collect2: ld returned 1 exit status
make[1]: * [pddrive] Error 1
make[1]: Leaving directory `/home/gaurish108/Desktop/ResearchMeetings/SUPERPETS/SuperLU_DIST_2.4/EXAMPLE'
make: * [example] Error 2
What should I do ???? I found that there is no ld folder in my /usr/bin/......Should I modify my make.inc file in some way?
Take a look here: http://www.network-theory.co.uk/docs/gccintro/gccintro_21.html. -lm says "search for libm.a in the search path". libm.a is found in /usr/lib/libm.a.

Resources