Basic Cmake and make on Mac OS Maverick for C - c

I'm a complete beginner in using Cmake to compile my own C code and I'm running Mac OS X Mavericks.
My problem is as follow. For my intro to Cmake, created a folder containing the following folders
bin/
build/
src/
fonctions.h
fonctions.c
main.c
Here my CMakeLists.txt file:
project(Addition)
add_executable(
bin/addition
src/main.c
src/fonctions.h
src/fonctions.c
)
When I run:
$ cmake . -G "Unix Makefiles"
and then make from my root folder, no problem.
But when I run:
$ cmake .. -G "Unix Makefiles"
and then make from my /build folder I got this error message:
Linking C executable bin/addition
ld: can't open output file for writing: bin/addition, errno=2 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [bin/addition] Error 1
make[1]: *** [CMakeFiles/bin/addition.dir/all] Error 2
make: *** [all] Error 2
Anyone knows why?

Remove bin/ from your add_executable and use CMAKE_RUNTIME_OUTPUT_DIRECTORY to change your build location
add_executable(
addition
src/main.c
src/fonctions.h
src/fonctions.c
)

Related

CMake build - ld reports collect2 error, cannot find library, but paths to library are correctly set [duplicate]

This question already has answers here:
usr/bin/ld: cannot find -l<nameOfTheLibrary>
(14 answers)
CMake link to external library
(6 answers)
Closed 1 year ago.
I am using CMake 3.19
I am compiling a C app that imports a third party shared library in the ${PROJECT_SOURCE_DIRECTORY}/lib folder.
Here is the relevant snippet of my CMakeLists.txt:
target_link_directories(TargetApp PUBLIC "/usr/local/lib"
${PROJECT_SOURCE_DIRECTORY}/lib )
get_target_property(OUT TargetApp LINK_DIRECTORIES)
#message("directories:" ${OUT})
target_link_libraries(TargetApp
"m"
"coolfoo"
)
get_target_property(OUT TargetApp LINK_LIBRARIES)
#message("linked libs:" ${OUT})
The messages on the console show that the link paths are correct, and that the correct libraries are also added correctly.
Last, but not least, I used ldd to check whether there were any missing symbols in the the third party library (there were no missing symbols).
Here is the ldd output:
ldd ../lib/coolfoo.so
linux-vdso.so.1 (0x00007ffd63d5f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f81966c5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f81964d3000)
/lib64/ld-linux-x86-64.so.2 (0x00007f81969ac000)
When I run make on the CMake Makefile, I get the following error (at the link stage):
[ 16%] Linking C executable TargetApp
/usr/bin/ld: cannot find -lcoolfoo
collect2: error: ld returned 1 exit status
Why is ld not able to find my shared library? and how do I resolve this?
[[Edit]]
Output of make V=1
[Fri, 08Oct21 12:49 minime#yourbox]:build$ make V=1
/home/minime/.local/bin/cmake -S/path/to/proj -B/path/to/proj/build --check-build-system CMakeFiles/Makefile.cmake 0
/home/minime/.local/bin/cmake -E cmake_progress_start /path/to/proj/build/CMakeFiles /path/to/proj/build//CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/path/to/proj/build'
make -f CMakeFiles/TargetApp.dir/build.make CMakeFiles/TargetApp.dir/depend
make[2]: Entering directory '/path/to/proj/build'
cd /path/to/proj/build && /home/minime/.local/bin/cmake -E cmake_depends "Unix Makefiles" /path/to/proj /path/to/proj /path/to/proj/build /path/to/proj/build /path/to/proj/build/CMakeFiles/TargetApp.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/path/to/proj/build'
make -f CMakeFiles/TargetApp.dir/build.make CMakeFiles/TargetApp.dir/build
make[2]: Entering directory '/path/to/proj/build'
[ 16%] Linking C executable TargetApp
/home/minime/.local/bin/cmake -E cmake_link_script CMakeFiles/TargetApp.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/TargetApp.dir/src/server/example-server.c.o CMakeFiles/TargetApp.dir/src/server/dirlookup.pb-c.c.o CMakeFiles/TargetApp.dir/src/server/query.pb-c.c.o -o TargetApp -L/usr/local/lib -L/path/to/proj/lib -Wl,-rpath,/usr/local/lib:/path/to/proj/lib -lm -lprotobuf-c -lprotobuf-c-rpc -lcoolfoo
/usr/bin/ld: cannot find -lcoolfoo
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TargetApp.dir/build.make:151: TargetApp] Error 1
make[2]: Leaving directory '/path/to/proj/build'
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/TargetApp.dir/all] Error 2
make[1]: Leaving directory '/path/to/proj/build'
make: *** [Makefile:103: all] Error 2
On Linux when you ask the linker to link with library coolfoo (with e.g. -lcoolfoo) the linker will look for a file named libcoolfoo.so.
The lib prefix is mandatory when looking for libraries using the -l option.
Rename (or build) your library as libcoolfoo.so and the linker should find it. Or use the full path to the actual library file (${PROJECT_SOURCE_DIRECTORY}/lib/coolfoo.so).

Error while compiling with cmake: cannot find -ldwmapi

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

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.

CMake: cannot compile out of source project with subprojects

I have a project (with 3 subprojects) with CMake that builds perfectly when I execute cmake in the same directory as the top CMakeLists.txt. But when I try an out of source build I get the following error:
javsalgar#javsalgar-laptop:~/LocalWork/eclipse_workspace/mpiperf_pi$ mkdir build
javsalgar#javsalgar-laptop:~/LocalWork/eclipse_workspace/mpiperf_pi$ cd build/
javsalgar#javsalgar-laptop:~/LocalWork/eclipse_workspace/mpiperf_pi/build$ ls
javsalgar#javsalgar-laptop:~/LocalWork/eclipse_workspace/mpiperf_pi/build$ cmake ..
-- The C compiler identification is GNU 4.7.3
-- The CXX compiler identification is GNU 4.7.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build
javsalgar#javsalgar-laptop:~/LocalWork/eclipse_workspace/mpiperf_pi/build$ make
/usr/bin/cmake -H/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi -B/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/CMakeFiles /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build'
make -f pi_mpireduce/CMakeFiles/pi_mpireduce.dir/build.make pi_mpireduce/CMakeFiles/pi_mpireduce.dir/depend
make[2]: Entering directory `/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build'
cd /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/pi_mpireduce /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/pi_mpireduce /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/pi_mpireduce/CMakeFiles/pi_mpireduce.dir/DependInfo.cmake --color=
Scanning dependencies of target pi_mpireduce
make[2]: Leaving directory `/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build'
make -f pi_mpireduce/CMakeFiles/pi_mpireduce.dir/build.make pi_mpireduce/CMakeFiles/pi_mpireduce.dir/build
make[2]: Entering directory `/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build'
/usr/bin/cmake -E cmake_progress_report /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/CMakeFiles 1
[ 20%] Building C object pi_mpireduce/CMakeFiles/pi_mpireduce.dir/src/mpi_pi_reduce.c.o
cd /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/pi_mpireduce && mpicc -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/include -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/pi_mpireduce -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/pi_mpireduce/include -o CMakeFiles/pi_mpireduce.dir/src/mpi_pi_reduce.c.o -c /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/pi_mpireduce/src/mpi_pi_reduce.c
/usr/bin/cmake -E cmake_progress_report /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/CMakeFiles 2
[ 40%] Building C object pi_mpireduce/CMakeFiles/pi_mpireduce.dir/src/dboard.c.o
cd /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/pi_mpireduce && mpicc -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/include -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/pi_mpireduce -I/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/pi_mpireduce/include -o CMakeFiles/pi_mpireduce.dir/src/dboard.c.o -c /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/pi_mpireduce/src/dboard.c
Linking C executable .
cd /home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build/pi_mpireduce && /usr/bin/cmake -E cmake_link_script CMakeFiles/pi_mpireduce.dir/link.txt --verbose=1
mpicc CMakeFiles/pi_mpireduce.dir/src/mpi_pi_reduce.c.o CMakeFiles/pi_mpireduce.dir/src/dboard.c.o -o . -rdynamic -lmpi
/usr/bin/ld: cannot open output file .: Is a directory
collect2: error: ld returned 1 exit status
make[2]: *** [pi_mpireduce] Error 1
make[2]: Leaving directory `/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build'
make[1]: *** [pi_mpireduce/CMakeFiles/pi_mpireduce.dir/all] Error 2
make[1]: Leaving directory `/home/javsalgar/LocalWork/eclipse_workspace/mpiperf_pi/build'
make: *** [all] Error 2
I found out what the problem was. The executable and the directory of the subproject had the same name. Chaning the executable name did the trick.
The line
mpicc CMakeFiles/pi_mpireduce.dir/src/mpi_pi_reduce.c.o CMakeFiles/pi_mpireduce.dir/src/dboard.c.o -o . -rdynamic -lmpi
tells the linker that the output name of the compilation shall be ., which is not a valid name for an executable or library or anything at all. This may come from you setting a target name from a variable that is empty during configure time or you accidentally wrote a . there.

C development under NETBEANS, cc1

I installed netbeans to write code in C. But there is problem using Mingw compilers. It works just fine, till i try to run the project.
First it was unable to find cc1.exe while trying to run it. When i found it and manually copied into the same folder there is just another error.
"C:/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
C:\bin\make.exe[1]: Entering directory `D:/1/2/Skola/IZP/programovani/projekt4netbeans/CppApplication_6'
"C:/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_1-Windows/cppapplication_6.exe
C:\bin\make.exe[2]: Entering directory `D:/1/2/Skola/IZP/programovani/projekt4netbeans/CppApplication_6'
mkdir -p build/Debug/MinGW_1-Windows
rm -f build/Debug/MinGW_1-Windows/main.o.d
gcc.exe -c -g -MMD -MP -MF build/Debug/MinGW_1-Windows/main.o.d -o build/Debug/MinGW_1-Windows/main.o main.c
cc1: error: command line option '-lang-c' is valid for the driver but not for C
C:\bin\make.exe[2]: *** [build/Debug/MinGW_1-Windows/main.o] Error 1
C:\bin\make.exe[2]: Leaving directory `D:/1/2/Skola/IZP/programovani/projekt4netbeans/CppApplication_6'
C:\bin\make.exe[1]: *** [.build-conf] Error 2
C:\bin\make.exe[1]: Leaving directory `D:/1/2/Skola/IZP/programovani/projekt4netbeans/CppApplication_6'
C:\bin\make.exe: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
if someone could help me with that.. thanks :)
Make shure you have current versions of Netbeans and MinGw and that you have a correct installation (Configuring the NetBeans IDE for C/C++/Fortran).
On mingw you should not use make from mingw/bin, but the one from msys (<mingw>/msys/1.0/bin/make.exe)

Resources