Why won't a CMake-generated Makefile find an external library? - c++

I'm attempting to use CMake for the first time and meeting with little success. My CMakeLists.txt is:
# Sets the version of CMake that is required.
cmake_minimum_required(VERSION 2.8.10)
# Name of the project.
project(QTCODERLIB)
# Adds common directories to the build.
include_directories(/usr/local/include)
link_directories(/usr/local/lib)
# Check for header files that we depend on.
include(CheckIncludeFiles)
check_include_files(libavcodec/avcodec.h HAVE_AVCODEC_H)
# Adds all of the source files.
file(GLOB SOURCE RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cxx)
# Builds a library named `qtcoder` from source.
add_library(qtcoder SHARED ${SOURCE})
# Links the library against third-party dependencies.
target_link_libraries(qtcoder avcodec)
My test.cxx file is:
#include "config.h"
#include <libavcodec/avcodec.h>
void f(void) {
avcodec_register_all();
}
cmake . && make VERBOSE=1 results in this:
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -H/Users/phowes/Personal/QTCoder/QTCoderLib -B/Users/phowes/Personal/QTCoder/QTCoderLib --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_progress_start /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make -f CMakeFiles/qtcoder.dir/build.make CMakeFiles/qtcoder.dir/depend
cd /Users/phowes/Personal/QTCoder/QTCoderLib && /usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_depends "Unix Makefiles" /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles/qtcoder.dir/DependInfo.cmake --color=
make -f CMakeFiles/qtcoder.dir/build.make CMakeFiles/qtcoder.dir/build
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_progress_report /Users/phowes/Personal/QTCoder/QTCoderLib/CMakeFiles 1
[100%] Building CXX object CMakeFiles/qtcoder.dir/test.cxx.o
/usr/bin/c++ -Dqtcoder_EXPORTS -fPIC -I/usr/local/include -o CMakeFiles/qtcoder.dir/test.cxx.o -c /Users/phowes/Personal/QTCoder/QTCoderLib/test.cxx
Linking CXX shared library libqtcoder.dylib
/usr/local/Cellar/cmake/2.8.10.1/bin/cmake -E cmake_link_script CMakeFiles/qtcoder.dir/link.txt --verbose=1
/usr/bin/c++ -dynamiclib -Wl,-headerpad_max_install_names -o libqtcoder.dylib -install_name /Users/phowes/Personal/QTCoder/QTCoderLib/libqtcoder.dylib CMakeFiles/qtcoder.dir/test.cxx.o -L/usr/local/lib -lavcodec
Undefined symbols for architecture x86_64:
"avcodec_register_all()", referenced from:
f() in test.cxx.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]: *** [libqtcoder.dylib] Error 1
make[1]: *** [CMakeFiles/qtcoder.dir/all] Error 2
make: *** [all] Error 2
Both the object file and the library are compiled as 64-bit:
$ lipo -info CMakeFiles/qtcoder.dir/test.cxx.o
Non-fat file: CMakeFiles/qtcoder.dir/test.cxx.o is architecture: x86_64
$ lipo -info /usr/local/lib/libavcodec.dylib
Non-fat file: /usr/local/lib/libavcodec.dylib is architecture: x86_64
The function exists in the library:
$ nm /usr/local/lib/libavcodec.dylib | grep avcodec_register_all
0000000000033c01 T _avcodec_register_all
What am I doing wrong here?

The avcodec libary is a C library and looking at your cmake output you are compiling for CXX. This question addresses how to handle precisely that scenario: g++ Linking Error on Mac while compiling FFMPEG

Usually you'd use find_package to do these sorts of heavy lifting tasks. CMake ships with a lot of modules by default, but sometimes you have to download one for less used projects.
Here is some documentation on how to find libraries.
Here is their contrived example of what you'd expect to see.
cmake_minimum_required(VERSION 2.8)
project(helloworld)
add_executable(helloworld hello.c)
find_package (BZip2)
if (BZIP2_FOUND)
include_directories(${BZIP_INCLUDE_DIRS})
target_link_libraries (helloworld ${BZIP2_LIBRARIES})
endif (BZIP2_FOUND)
From simple web searches it isn't clear to me that CMake provides an AVCodec module. I found a few online:
https://github.com/arjanhouben/SDL_ffmpeg/blob/master/Findavcodec.cmake
http://whispercast.org/trac/browser/trunk/cmake/FindLibAvCodec.cmake
I see most people using this library are interested in FFMPEG, which maybe is what you are interested too.
https://github.com/zinnschlag/openmw/blob/master/cmake/FindFFMPEG.cmake
If you do need to use one that you download, here are some instructions for how to use custom modoules.

Related

cmake on Mac with ARM M1 is running linker with x86_64 architecture instead of arm64

I am trying to compile glfw from source on Mac with M1 arm64 processor, and while running the linker, cmake strangely is trying to link the project for x86_64 architecture, while the binaries were built for arm64.
I clone the project, create build folder named cmake-build-debug, generate build system in it with the Makefile etc. as follows:
git clone https://github.com/glfw/glfw.git
cd glfw
mkdir cmake-build-debug
cd cmake-build-debug
cmake -S .. -B . -DCMAKE_BUILD_TYPE=Debug -DCMAKE_HOST_SYSTEM_PROCESSOR=arm64 -DCMAKE_SYSTEM_PROCESSOR=arm64
This works fine. But now that I build it with make or cmake --config Debug --build ., the .o binaries are generated perfectly fine, but linker script is incorrectly invoked by cmake with x86_64 target architecture for some reason:
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Including Cocoa support
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/burkov/Documents/Projects/open-source/glfw/cmake-build-debug
[ 47%] Built target glfw
Scanning dependencies of target wave
[ 50%] Linking C executable wave.app/Contents/MacOS/wave
ld: warning: ignoring file CMakeFiles/wave.dir/wave.c.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file ../src/libglfw3.a, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
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]: *** [examples/wave.app/Contents/MacOS/wave] Error 1
make[1]: *** [examples/CMakeFiles/wave.dir/all] Error 2
make: *** [all] Error 2
I look at the failing Makefile in glfw/cmake-build-debug/examples/CMakeFiles/wave.dir/build.make and see the line, where cmake is crashing:
cd /Users/me/Documents/Projects/open-source/glfw/cmake-build-debug/examples && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/wave.dir/link.txt --verbose=$(VERBOSE)
I manually open the file glfw/cmake-build-debug/examples/CMakeFiles/wave.dir/link.txt file and see the following link script code there:
/Library/Developer/CommandLineTools/usr/bin/cc -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/wave.dir/wave.c.o -o wave.app/Contents/MacOS/wave ../src/libglfw3.a -framework Cocoa -framework IOKit -framework CoreFoundation
If I manually execute this line from shell, it successfully builds my binary for arm64 architecture, as expected.
But when this link.txt script is automatically invoked with cmake via cmake -E cmake_link_script CMakeFiles/wave.dir/link.txt --verbose=$(VERBOSE), it fails, apparently, trying to build the binary for the wrong x86_64 architecture.
Why is this happening and how to fix this?
For anyone running into the same problem, it looks like the first version of cmake with an adequate support for Apple Silicon is 3.19.
I was using 3.17.5 as my slightly out-of-date version of CLion does not support versions of cmake above that.
After an update to cmake 3.22.4 the problem is gone.

How to fix "ld: symbol(s) not found for architecture x86_64" using C++, CMake and Tensorflow?

I created a test project in C ++ using TensorFlow with CMake. But I have an error:
ld: symbol(s) not found for architecture x86_64
I think, I have an error in my CMake files. When I try compile via the terminal with gcc tensortest.cpp -ltensorflow -o tf, everything works fine.
I have two CMake files.
FindTensorFlow.cmake:
# Locates the tensorFlow library and include directories.
include(FindPackageHandleStandardArgs)
unset(TENSORFLOW_FOUND)
find_path(TensorFlow_INCLUDE_DIR
NAMES
tensorflow
HINTS
/usr/local/include/tensorflow)
find_library(TensorFlow_LIBRARY
NAMES
libtensorflow_framework.1.14.0.dylib
libtensorflow_framework.1.dylib
libtensorflow_framework.dylib
libtensorflow.1.14.0.dylib
libtensorflow.1.dylib
libtensorflow.dylib
HINTS
/usr/local/lib)
# set TensorFlow_FOUND
find_package_handle_standard_args(TensorFlow DEFAULT_MSG TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)
# set external variables for usage in CMakeLists.txt
if(TENSORFLOW_FOUND)
set(TensorFlow_LIBRARIES ${TensorFlow_LIBRARY})
set(TensorFlow_INCLUDE_DIRS ${TensorFlow_INCLUDE_DIR})
endif()
# hide locals from GUI
mark_as_advanced(TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(TensorFlowTest)
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
find_package(TensorFlow REQUIRED)
include_directories(${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})
add_executable(TensorFlowTest main.cpp)
target_link_libraries(TensorFlowTest ${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})
And one main.cpp
#include <stdio.h>
#include <tensorflow/c/c_api.h>
int main() {
printf("Hello from TensorFlow C library version %s\n", TF_Version());
return 0;
}
When reload project, I have a CMake message:
-- Found TensorFlow: /usr/local/include
-- Configuring done
WARNING: Target "TensorFlowTest" requests linking to directory "/usr/local/include". Targets may link only to libraries. CMake is dropping the item.
-- Generating done
-- Build files have been written to: /Users/neikr/CLionProjects/TensorFlowTest/cmake-build-debug
And when compiling:
Scanning dependencies of target TensorFlowTest
[ 50%] Building CXX object CMakeFiles/TensorFlowTest.dir/main.cpp.o
[100%] Linking CXX executable TensorFlowTest
Undefined symbols for architecture x86_64:
"_TF_Version", referenced from:
_main in main.cpp.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[3]: *** [TensorFlowTest] Error 1
make[2]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make[1]: *** [CMakeFiles/TensorFlowTest.dir/rule] Error 2
make: *** [TensorFlowTest] Error 2
Update
I tried make with VERBOSE=1 and I get this output:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -S/Users/neikr/CLionProjects/TensorFlowTest -B/Users/neikr/CLionProjects/TensorFlowTest --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_progress_start /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/depend
cd /Users/neikr/CLionProjects/TensorFlowTest && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_depends "Unix Makefiles" /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles/TensorFlowTest.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/build
[ 50%] Linking CXX executable TensorFlowTest
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/TensorFlowTest.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/TensorFlowTest.dir/main.cpp.o -o TensorFlowTest -Wl,-rpath,/usr/local/lib /usr/local/lib/libtensorflow_framework.1.14.0.dylib
Undefined symbols for architecture x86_64:
"_TF_Version", referenced from:
_main in main.cpp.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]: *** [TensorFlowTest] Error 1
make[1]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make: *** [all] Error 2
Also, I tried adding message("TensorFlow_LIBRARIES: ${TensorFlow_LIBRARIES}") to the end of my CMake and I get this message:
TensorFlow_LIBRARIES: /usr/local/lib/libtensorflow_framework.1.14.0.dylib
Based on your print-out from the message() command, it is more clear what is going on. In your FindTensorFlow.cmake file, the find_library() call only finds one library (libtensorflow_framework.1.14.0.dylib), which is expected behavior:
Once one of the calls succeeds the result variable will be set and stored in the cache so that no call will search again.
When more than one value is given to the NAMES option this command by default will consider one name at a time and search every directory for it.
Thus, once find_library() finds the libtensorflow_framework library, the search stops. If you want to also find libtensorflow.dylib, which may help with your link error, you have to specify another find_library() call, using a different TensorFlow_ variable. So changing that section of your FindTensorFlow.cmake to something like this should help:
find_library(TensorFlow_LIBRARY
NAMES
libtensorflow.1.14.0.dylib
libtensorflow.1.dylib
libtensorflow.dylib
HINTS
/usr/local/lib)
find_library(TensorFlow_FRAMEWORK_LIBRARY
NAMES
libtensorflow_framework.1.14.0.dylib
libtensorflow_framework.1.dylib
libtensorflow_framework.dylib
HINTS
/usr/local/lib)
# set TensorFlow_FOUND
find_package_handle_standard_args(TensorFlow DEFAULT_MSG
TensorFlow_INCLUDE_DIR
TensorFlow_LIBRARY
TensorFlow_FRAMEWORK_LIBRARY
)

Cross compiling with clang on cmake, linker does not support armelf_linux_eabi

Here's my cmake toolchain file to cross compile for arm linux:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CLANG_TARGET_TRIPLE arm-linux-gnueabihf)
set(GCC_ARM_TOOLCHAIN_PREFIX ${CLANG_CLANG_TARGET_TRIPLE})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_ASM_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
Here's the problem when I execute cmake with it:
CMake Error at /usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
The C compiler
"/usr/bin/clang"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/lz/orwell/gtk/build/armlinux/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_8132a/fast"
/usr/bin/make -f CMakeFiles/cmTC_8132a.dir/build.make CMakeFiles/cmTC_8132a.dir/build
make[1]: Entering directory '/home/lz/orwell/gtk/build/armlinux/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_8132a.dir/testCCompiler.c.o
/usr/bin/clang --target=arm-linux-gnueabihf -o CMakeFiles/cmTC_8132a.dir/testCCompiler.c.o -c /home/lz/orwell/gtk/build/armlinux/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_8132a
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_8132a.dir/link.txt --verbose=1
/usr/bin/clang --target=arm-linux-gnueabihf -rdynamic CMakeFiles/cmTC_8132a.dir/testCCompiler.c.o -o cmTC_8132a
/usr/bin/ld: unrecognised emulation mode: armelf_linux_eabi
Supported emulations: elf_x86_64 elf32_x86_64 elf_i386 elf_iamcu i386linux elf_l1om elf_k1om i386pep i386pe
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/cmTC_8132a.dir/build.make:97: recipe for target 'cmTC_8132a' failed
make[1]: *** [cmTC_8132a] Error 1
make[1]: Leaving directory '/home/lz/orwell/gtk/build/armlinux/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_8132a/fast' failed
make: *** [cmTC_8132a/fast] Error 2
It looks like it's trying to use /bin/ld instead of clang's linker (is there one?). I know that there exists CMAKE_LANG_COMPILER_EXTERNAL_TOOLCHAIN but I don't know the path to include and preferably I didn't want to include any paths, I'd like to make it work without path specification.
So, how to specify a compatible linker?

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

Cross compiling libwebsockets with cmake

I am trying to cross-compile libwebsockets with cmake, but I am getting the below errors:
root#ubuntu-Latitude-E5450:/home/ubuntu/Downloads/libwebsockets/build# cmake .. -DCMAKE_INSTALL_PREFIX_PATH=/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/ -DCMAKE_LIBRARY_PATH=/home/ubuntu/toolchain/sysroots/aarch64-oe-linux/usr/lib/ -DCMAKE_SYSROOT=/ubuntu/home/das_toolchain/sysroots/aarch64-oe-linux/ -DCMAKE_TOOLCHAIN_FILE=../cross-arm-oe-linux-gnueabi.cmake -DLWS_WITHOUT_EXTENSIONS=1 -DLWS_WITH_SSL=0 -DLWS_WITH_ZIP_FOPS=0 -DLWS_WITH_ZLIB=0
-- The C compiler identification is GNU 4.9.3
-- Check for working C compiler: /home/ubuntu/das_toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-gcc
-- Check for working C compiler: /home/ubuntu/das_toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-gcc -- broken
CMake Error at /usr/local/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler
"/home/ubuntu/das_toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/ubuntu/Downloads/libwebsockets/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_515b7/fast"
/usr/bin/make -f CMakeFiles/cmTC_515b7.dir/build.make
CMakeFiles/cmTC_515b7.dir/build
make[1]: Entering directory
`/home/ubuntu/Downloads/libwebsockets/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_515b7.dir/testCCompiler.c.o
/home/ubuntu/das_toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-gcc
--sysroot=/ubuntu/home/das_toolchain/sysroots/aarch64-oe-linux/ -o
CMakeFiles/cmTC_515b7.dir/testCCompiler.c.o -c
/home/ubuntu/Downloads/libwebsockets/build/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_515b7
/usr/local/bin/cmake -E cmake_link_script
CMakeFiles/cmTC_515b7.dir/link.txt --verbose=1
/home/ubuntu/das_toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/arm-oe-linux-gnueabi-gcc
--sysroot=/ubuntu/home/das_toolchain/sysroots/aarch64-oe-linux/ -rdynamic
CMakeFiles/cmTC_515b7.dir/testCCompiler.c.o -o cmTC_515b7
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot open crt1.o: No such file or directory
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot open crti.o: No such file or directory
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot open crtbegin.o: No such file or directory
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot open crtend.o: No such file or directory
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot open crtn.o: No such file or directory
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot find -lgcc
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot find -lgcc_s
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot find -lc
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot find -lgcc
/home/ubuntu/toolchain/sysroots/x86_64-oesdk-linux/usr/libexec/arm-oe-linux-gnueabi/gcc/arm-oe-linux-gnueabi/4.9.3/real-ld:
error: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
make[1]: *** [cmTC_515b7] Error 1
make[1]: Leaving directory
`/home/ubuntu/Downloads/libwebsockets/build/CMakeFiles/CMakeTmp'
make: *** [cmTC_515b7/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:146 (project)
-- Configuring incomplete, errors occurred!
See also "/home/ubuntu/Downloads/libwebsockets/build/CMakeFiles/CMakeOutput.log".
See also "/home/ubuntu/Downloads/libwebsockets/build/CMakeFiles/CMakeError.log".
I have specified the CMAKE_SYSROOT in the toolchain file and also specified the same in my command line while building. Where am I going wrong with this?
Your help would be appreciated.
Below is my toolchain file:
#
# CMake Toolchain file for crosscompiling on ARM.
#
# This can be used when running cmake in the following way:
# cd build/
# cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm-oe-linux-gnueabi.cmake
#
set(CROSS_COMPILER_PATH /home/ubuntu/das_toolchain/sysroots/x86_64-oesdk-linux/usr/bin/arm-oe-linux-gnueabi/)
set(CROSS_PATH /ubuntu/home/das_toolchain/sysroots/aarch64-oe-linux/)
set(TOOLCHAIN_PREFIX arm-oe-linux-gnueabi-)
#set(SYSROOT --sysroot=/ubuntu/home/das_toolchain/sysroots/aarch64-oe-linux/)
set(CMAKE_SYSROOT /ubuntu/home/das_toolchain/sysroots/aarch64-oe-linux/)
# Target operating system name.
#set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_NAME Arm)
# Name of C compiler.
#set(CMAKE_C_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-gcc")
#set(CMAKE_CXX_COMPILER "${CROSS_PATH}/bin/arm-linux-gnueabihf-g++")
#set(CMAKE_C_COMPILER "${CROSS_PATH}${TOOLCHAIN_PREFIX}gcc ${SYSROOT}")
#set(CMAKE_CXX_COMPILER "${CROSS_PATH}${TOOLCHAIN_PREFIX}g++ ${SYSROOT}")
set(CMAKE_C_COMPILER "${CROSS_COMPILER_PATH}${TOOLCHAIN_PREFIX}gcc")
set(CMAKE_CXX_COMPILER "${CROSS_COMPILER_PATH}${TOOLCHAIN_PREFIX}g++")
#set(CMAKE_C_FLAGS ${SYSROOT})
#set(CMAKE_CXX_FLAGS ${SYSROOT})
# Where to look for the target environment. (More paths can be added here)
set(CMAKE_FIND_ROOT_PATH "${CROSS_PATH}")
# Adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment only.
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search headers and libraries in the target environment only.
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
In my experience, using CMAKE_SYSROOT can cause issues like this sometimes since it passes the --sysroot flag to the compiler, which can confuse it. To tell CMake where to find libraries, without affecting the actual compiler options, just set CMAKE_PREFIX_PATH instead of CMAKE_SYSROOT.