CMake ARM GCC Undefined reference to "_exit" [duplicate] - c++

This question already has answers here:
CMake: The C Compiler is not able to compile a simple test program
(4 answers)
Linker error on a C project using Eclipse
(5 answers)
How to cross compile with cmake + arm-none-eabi on windows?
(1 answer)
Closed 1 year ago.
I am trying to use arm-none-eabi-gcc with CMake but am getting an error about an undefined reference to "_exit". The following is the terminal output after running cmake (the first line is the command):
cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=F:\Resources\Dev\Tools\GNU-arm\10.3-2021.07\bin\arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=F:\Resources\Dev\Tools\GNU-arm\10.3-2021.07\bin\arm-none-eabi-g++.exe -Hf:/Projects/Dev/VS-Code/CPP/CPP-Sandbox "-Bf:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug" -G Ninja
Not searching for unused variables given on the command line.
-- The C compiler identification is GNU 10.3.1
-- Check for working C compiler: F:/Resources/Dev/Tools/GNU-arm/10.3-2021.07/bin/arm-none-eabi-gcc.exe
-- Check for working C compiler: F:/Resources/Dev/Tools/GNU-arm/10.3-2021.07/bin/arm-none-eabi-gcc.exe - broken
CMake Error at C:/Program Files/CMake/share/cmake-3.17/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"F:/Resources/Dev/Tools/GNU-arm/10.3-2021.07/bin/arm-none-eabi-gcc.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: F:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug/CMakeFiles/CMakeTmp
Run Build Command(s):F:/Resources/Dev/Tools/MinGW/MinGW-9.0.0-x64/bin/ninja.exe cmTC_1a12b && [1/2] Building C object CMakeFiles/cmTC_1a12b.dir/testCCompiler.c.obj
[2/2] Linking C executable cmTC_1a12b
FAILED: cmTC_1a12b
cmd.exe /C "cd . && F:\Resources\Dev\Tools\GNU-arm\10.3-2021.07\bin\arm-none-eabi-gcc.exe CMakeFiles/cmTC_1a12b.dir/testCCompiler.c.obj -o cmTC_1a12b && cd ."
f:/resources/dev/tools/gnu-arm/10.3-2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: f:/resources/dev/tools/gnu-arm/10.3-2021.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib\libc.a(lib_a-exit.o): in function `exit':
exit.c:(.text.exit+0x2c): undefined reference to `_exit'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:41 (project)
-- Configuring incomplete, errors occurred!
See also "F:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug/CMakeFiles/CMakeOutput.log".
See also "F:/Projects/Dev/VS-Code/CPP/CPP-Sandbox/build/GCC 10.3.1 arm-none-eabi/Debug/CMakeFiles/CMakeError.log".
I have tried the following solution (i.e. adding simply adding -specs=nosys.specs to my CMakeLists.txt but to no avail. Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_STANDARD 99)
# Project variables
set(LOCAL_PROJECT_NAME "C-Sandbox")
set(LOCAL_PROJECT_VERSION "0.0.1")
set(LOCAL_PROJECT_DESCRIPTION "Description")
# Project setup
project(${LOCAL_PROJECT_NAME}
VERSION ${LOCAL_PROJECT_VERSION}
DESCRIPTION ${LOCAL_PROJECT_DESCRIPTION}
LANGUAGES C)
add_executable(${LOCAL_PROJECT_NAME})
target_sources(${LOCAL_PROJECT_NAME} PRIVATE src/main.c)
target_link_options(${LOCAL_PROJECT_NAME} PRIVATE "-specs=nosys.specs")
set_target_properties(${LOCAL_PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "bin")
Edit:
When I cd into my source folder and run arm-none-eabi-gcc main.c -lc "-specs=nosys.specs" I get no error and an a.out file.

Related

System Cannot find the file nmake cmake error with Mingw [duplicate]

This question already has answers here:
Error while configuring CMake project: Running 'nmake' '-?' failed
(2 answers)
Closed 4 months ago.
When building with cmake on windows using Mingw64 we get errors like:
System cannot find the file specified
Log:
$ cmake ..
-- Building for: NMake Makefiles
CMake Error at CMakeLists.txt:72 (project):
Running
'nmake' '-?'
failed with:
The system cannot find the file specified
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "E:/MyTools/C/libs_download/cpp-httplib-0.11.2/build/CMakeFiles/CMakeOutput.log".
How to fix it?
This happens because cmake uses the Microsoft CMake format as default. The CMAKE_CXX_COMPILER not set here also indicates that we need to set the CMAKE_CXX_COMPILER variable to a c++ compiler path.
To fix we need to use the following command:
cmake -DCMAKE_CXX_COMPILER=g++ -DCMAKE_CC_COMPILER=gcc -DCMAKE_MAKE_PROGRAM=mingw32-make -G "MinGW Makefiles" ..
Here we provide the following flags:
CMAKE_CXX_COMPILER=g++ - Use g++ as C++ compiler (or provide full path)
CMAKE_CC_COMPILER=gcc - Use gcc as C compiler
CMAKE_MAKE_PROGRAM=mingw32-make - Use mingw32-make as the make program
-G "MinGW Makefiles" - Use the MinGW Makefile format not Microsoft makefile format
After this you can run mingw32-make as usual instead of make to build :)

CMake can't find stdc++11 after Xcode 10 update

I have a CMake project using SFML which was working fine but after updating to Xcode 10, all of the compiler files that CMake looks for can't be found.
clang: warning: libstdc++ is deprecated; move to libc++
I'm not sure how to move to the different lib source.
I've tried using set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=libc++") to use the flag.
It also tells me: Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- broken. I can run the g++/c++ commands fine in the terminal. I assume they are being looked for in the same spot.
CMake file I'm trying to compile with. Its the SFML one:
SFML CMake
Full Error Message:
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- broken
CMake Error at /Applications/CMake.app/Contents/share/cmake-3.7/Modules/CMakeTestCXXCompiler.cmake:44 (message):
The C++ compiler
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /Users/DSchana/Documents/Libraries/SFML/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_af3d5/fast"
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f
CMakeFiles/cmTC_af3d5.dir/build.make CMakeFiles/cmTC_af3d5.dir/build
Building CXX object CMakeFiles/cmTC_af3d5.dir/testCXXCompiler.cxx.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-arch x86_64 -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.07.sdk
-mmacosx-version-min=10.7 -o
CMakeFiles/cmTC_af3d5.dir/testCXXCompiler.cxx.o -c
/Users/DSchana/Documents/Libraries/SFML/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
warning: include path for stdlibc++ headers not found; pass '-std=libc++'
on the command line to use the libc++ standard library instead
[-Wstdlibcxx-not-found]
1 warning generated.
Linking CXX executable cmTC_af3d5
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script
CMakeFiles/cmTC_af3d5.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-arch x86_64 -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.07.sdk
-mmacosx-version-min=10.7 -Wl,-search_paths_first
-Wl,-headerpad_max_install_names
CMakeFiles/cmTC_af3d5.dir/testCXXCompiler.cxx.o -o cmTC_af3d5
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum
deployment target of OS X 10.9 [-Wdeprecated]
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make[1]: *** [cmTC_af3d5] Error 1
make: *** [cmTC_af3d5/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:47 (project)
-- Configuring incomplete, errors occurred!
Ok. Turns out I just needed to add set(CMAKE_CXX_FLAGS "-stdlib=libc++") to my CMakeLists.txt

CMake "clang++ is not able compile a simple test program" (Fedora 20)

So I tried to install clang + cmake to compile a simple C++ program and I'm getting the following error:
-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is Clang 3.5.0
-- 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/local/bin/clang++
-- Check for working CXX compiler: /usr/local/bin/clang++ -- broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCXXCompiler.cmake:54 (message):
The C++ compiler "/usr/local/bin/clang++" is not able to compile a simple
test program.
It fails with the following output:
Change Dir: /home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/gmake "cmTryCompileExec697180971/fast"
/usr/bin/gmake -f CMakeFiles/cmTryCompileExec697180971.dir/build.make
CMakeFiles/cmTryCompileExec697180971.dir/build
gmake[1]: Entering directory
`/home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report
/home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp/CMakeFiles 1
Building CXX object
CMakeFiles/cmTryCompileExec697180971.dir/testCXXCompiler.cxx.o
/usr/local/bin/clang++ -o
CMakeFiles/cmTryCompileExec697180971.dir/testCXXCompiler.cxx.o -c
/home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Linking CXX executable cmTryCompileExec697180971
/usr/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec697180971.dir/link.txt --verbose=1
/usr/local/bin/clang++
CMakeFiles/cmTryCompileExec697180971.dir/testCXXCompiler.cxx.o -o
cmTryCompileExec697180971 -rdynamic
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
gmake[1]: Leaving directory
`/home/jtcwang/tmp/CMake/CMake/CMakeFiles/CMakeTmp'
gmake[1]: *** [cmTryCompileExec697180971] Error 1
gmake: *** [cmTryCompileExec697180971/fast] Error 2
It's not even compiling my program because it fails to compile a test program.
Looks like the important line is here:
/usr/bin/ld: cannot find -lstdc++
However, I have checked that libstdc++ is installed and up to date, so at this point I'm quite lost.
Other things I've tried:
Using prebuilt binaries instead of sudo yum install clang
remove and reinstall
Tried clang++ hello.cpp (hello world program). It says <iostreams> is not found. Is clang missing a standard library? EDIT: changing to <iostream> gives me the same linker error above.
I'm not familiar with the clang, cmake and C++ scene in general, so I'd appreciate any pointers. Thanks!
You need the development libraries and headers for C++ library, try
yum install libstdc++-devel
Your /home/gnu/bin/c++ seem to require additional flag to link things properly and CMake doesn't know about that.
To use /usr/bin/c++ as your compiler run cmake with -DCMAKE_CXX_COMPILER=/usr/bin/c++.
Also, CMAKE_PREFIX_PATH variable sets destination dir where your project' files should be installed. It has nothing to do with CMake installation prefix and CMake itself already know this.

cmake error showing nonexistent folder on running in cygwin terminal

I am trying to compile a c++ library g2o in cygwin(in windows 8) using cmake . I created a build folder and when I try to do cmake in that folder it fails with the following error
$ cmake ../
-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is GNU 4.8.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- broken
CMake Error at /usr/share/cmake-2.8.11.2/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "/usr/bin/cc" is not able to compile a simple test program.
It fails with the following output:
Change Dir: /cygdrive/b/g2o/trunk/build/CMakeFiles/CMakeTmp
Run Build Command:/cygdrive/c/Program\
Files/MATLAB/R2009a/bin/win64/gmake.exe "cmTryCompileExec3795381385/fast"
C:/Program Files/MATLAB/R2009a/bin/win64/gmake -f
CMakeFiles/cmTryCompileExec3795381385.dir/build.make
CMakeFiles/cmTryCompileExec3795381385.dir/build
gmake[1]: Entering directory `B:/g2o/trunk/build/CMakeFiles/CMakeTmp'
/usr/bin/cmake.exe -E cmake_progress_report "/cygdrive/b/g2o/trunk/build/CMakeFiles/CMakeTmp/CMakeFiles" 1
Building C object
CMakeFiles/cmTryCompileExec3795381385.dir/testCCompiler.c.o
/usr/bin/cc -o CMakeFiles/cmTryCompileExec3795381385.dir/testCCompiler.c.o
-c "/cygdrive/b/g2o/trunk/build/CMakeFiles/CMakeTmp/testCCompiler.c"
Linking C executable cmTryCompileExec3795381385.exe
/usr/bin/cmake.exe -E cmake_link_script
CMakeFiles/cmTryCompileExec3795381385.dir/link.txt --verbose=1
gmake[1]: Leaving directory `B:/g2o/trunk/build/CMakeFiles/CMakeTmp'
process_begin: CreateProcess(NULL, /usr/bin/cmake.exe -E cmake_link_script
CMakeFiles/cmTryCompileExec3795381385.dir/link.txt --verbose=1, ...)
failed.
make (e=2): The system cannot find the file specified.
gmake[1]: *** [cmTryCompileExec3795381385.exe] Error 2
gmake: *** [cmTryCompileExec3795381385/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:4 (PROJECT)
-- Configuring incomplete, errors occurred!
The interesting thing here is I have cmake 3.0 installed in my machine and the error says cmake 2.8.11.2. I did a search in the pc and cannot find any folder by that name. The system path variable contains the path to correct cmake bin folder also.
I cannot figure out what could be the reason behind this error. Can it be an issue with the cygwin itself? What can I do to avoid this error?
One thing to note, Cygwin has its own copy of CMake, which is what your seeing as 2.8.11.2. That version has nothing to do with the one you have on your Windows machine. Have you tried to use the Windows installed version and not Cygwin?
I think the problem comes from not having all required compiler libraries installed in Cygwin. You could try creating a simple C++ file and try compiling it with gcc directly. If that fails, you've now found out why CMake is failing.
Unless there is a good reason to do so, you should probably consider NOT using Cygwin to run CMake inside Windows. CMake is very capable of producing Visual Studio project files.
The issue, at least for me, was the c compiler not accepting unix style paths.
The solution was to install gcc via cygwin. The packages I installed is gcc-base and gcc-cpp I believe.

Building c++ project on Windows with CMake, Clang and Ninja

I currently have cmake, clang and ninja installed on windows. I am trying to use CMake to generate a ninja build file to compile a very simple hello world program.
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 2.8)
project(test_project)
add_executable(main main.cpp)
main.cpp is a simple hello world program.
On the command line I run this: cmake -G Ninja .. and I get the following errors:
-- The C compiler identification is Clang 3.5.0
clang.exe: error: no such file or directory: '/nologo'
clang.exe: error: no such file or directory: '/showIncludes'
-- The CXX compiler identification is Clang 3.5.0
clang.exe: error: no such file or directory: '/nologo'
clang.exe: error: no such file or directory: '/showIncludes'
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "C:/llvm_build/RelWithDebInfo/bin/clang.exe" is
not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/test_proj/build/CMakeFiles/CMakeTmp
Run Build Command:C:/ninja/ninja.exe cmTryCompileExec375034429
[1/2] Building C object
CMakeFiles\cmTryCompileExec375034429.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTryCompileExec375034429.exe
FAILED: cmd.exe /c cd . &&
C:\llvm_build\RelWithDebInfo\bin\clang.exe
CMakeFiles\cmTryCompileExec375034429.dir\testCCompiler.c.obj -o
cmTryCompileExec375034429.exe && cd .
clang.exe: error: unable to execute command: program not executable
clang.exe: error: linker command failed with exit code 1 (use -v to see
invocation)
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "C:/test_proj/build/CMakeFiles/CMakeOutput.log".
See also "C:/test_proj/build/CMakeFiles/CMakeError.log".
The CMakeError.log file looks like this:
Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: C:/llvm_build/RelWithDebInfo/bin/clang.exe
Build flags:
Id flags:
The output was:
1
clang.exe: error: unable to execute command: program not executable
clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)
Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: C:/llvm_build/RelWithDebInfo/bin/clang++.exe
Build flags:
Id flags:
The output was:
1
clang++.exe: error: unable to execute command: program not executable
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
Determining if the C compiler works failed with the following output:
Change Dir: C:/test_proj/build/CMakeFiles/CMakeTmp
Run Build Command:C:/ninja/ninja.exe cmTryCompileExec2120850158
[1/2] Building C object CMakeFiles\cmTryCompileExec2120850158.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTryCompileExec2120850158.exe
FAILED: cmd.exe /c cd . && C:\llvm_build\RelWithDebInfo\bin\clang.exe CMakeFiles\cmTryCompileExec2120850158.dir\testCCompiler.c.obj -o cmTryCompileExec2120850158.exe && cd .
clang.exe: error: unable to execute command: program not executable
clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
It appears that cmake is trying to test clang with windows options /nologo and /showIncludes. I cannot figure out how to tell cmake to pass the proper arguments.
FWIW I'm running 64bit Windows 7
EDIT:
So I looked through the built in cmake files and I found that the CMakeClDeps.cmake file was the culprit for adding the /nologo /showIncludes options. It appears that if I set Clang as the compiler then cmake thinks that visual studio is the compiler (it sets MSVC_C_ARCHITECTURE_ID to x86).
I removed the line in CMakeDetermineCompilerId.cmake that sets MSVC_C_ARCHITECTURE_ID and after trying again I get the following errors:
-- The C compiler identification is Clang 3.5.0
-- The CXX compiler identification is Clang 3.5.0
-- Check for working C compiler using: Ninja
-- Check for working C compiler using: Ninja -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "C:/llvm_build/RelWithDebInfo/bin/clang.exe" is
not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/test_proj/build/CMakeFiles/CMakeTmp
Run Build Command:C:/ninja/ninja.exe cmTryCompileExec2815594422
[1/2] Building C object
CMakeFiles\cmTryCompileExec2815594422.dir\testCCompiler.c.obj
[2/2] Linking C executable cmTryCompileExec2815594422.exe
FAILED: cmd.exe /c cd . &&
C:\llvm_build\RelWithDebInfo\bin\clang.exe
CMakeFiles\cmTryCompileExec2815594422.dir\testCCompiler.c.obj -o
cmTryCompileExec2815594422.exe && cd .
clang.exe: error: unable to execute command: program not executable
clang.exe: error: linker command failed with exit code 1 (use -v to see
invocation)
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "C:/test_proj/build/CMakeFiles/CMakeOutput.log".
See also "C:/test_proj/build/CMakeFiles/CMakeError.log".
Don't know if it can be helpful but I had the same error. Now I can compile with clang(3.7.1)/ninja(1.6)/cmake(3.4.1) on Windows performing the following actions in a build directory:
load the relevant vcvarsXX.bat file (e.g. "<Your Visual Studio location>\VC\vcvarsall.bat" x86)
set both CC and CXX to clang-cl (instead of clang and clang++)
run cmake -G Ninja <project>
run cmake --build .
Turns out the second set of errors I received were because clang could not find the linker. I had built clang using visual studio but at the time it couldn't find the visual studio linker. All I had to do was run it in the visual studio development console.
CMake still thinks that clang is a visual studio compiler so in the CMakeDetermineCompilerId.cmake file there is a line that looks like this:
set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
and I changed it to look like this
if (COMPILER_ID MATCHES "MSVC")
set(MSVC_${lang}_ARCHITECTURE_ID "${ARCHITECTURE_ID}")
endif()
Hopefully this doesn't break any other CMake functionality.