CMake : /usr/include not used to find fftw3.h - c++

I'm working on a personal project to cross compile for a Raspberry Pi with CMake. This is the first time I build a project with it.
One of the file I compile include the fftw3.h header. This file is present in the /usr/include folder as shown by the following output :
$ ls /usr/include/fftw3*
/usr/include/fftw3.f
/usr/include/fftw3.f03
/usr/include/fftw3.h
/usr/include/fftw3l.f03
/usr/include/fftw3q.f03
The file is successfully found by cmake with find_path(FFTW_FOLDER NAMES fftw3.h) and even if cmake looks in this path by default, I add it explicitly when constructing my target with target_include_directories(${LIB_NAME} PUBLIC /usr/include).
However, when comes the compilation, I have the classic error message telling me that fftw3.h is not found.
If I copy the file and put it in my home folder and add my home folder in the include list there is no problem and the compilation works.
Any thoughts ?
Thanks,
Damien
Edit 1 to answer #Pablo and #hikerjobs:
This is how my compile line looks like:
/home/littlemonster/projects/rpi-workspace/rpi-tools/arm-bcm2708/gcc-
linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++
-Dmatrix_hal_EXPORTS -I/home/littlemonster/projects/rpi-
workspace/lib/matrix-hal/matrix-creator-hal -fPIC -Wall -Werror -
Wextra -Wdouble-promotion -std=gnu++11 -o CMakeFiles/matrix-
hal.dir/matrix-creator-hal/cpp/driver/cross_correlation.cpp.o -c
/home/littlemonster/projects/rpi-workspace/lib/matrix-hal/matrix-
creator-hal/cpp/driver/cross_correlation.cpp
In file included from /home/littlemonster/projects/rpi-
workspace/lib/matrix-hal/matrix-creator-
hal/cpp/driver/cross_correlation.cpp:18:0:
/home/littlemonster/projects/rpi-workspace/lib/matrix-hal/matrix-
creator-hal/cpp/driver/cross_correlation.h:21:19: fatal error:
fftw3.h: No such file or directory
#include <fftw3.h>
^
compilation terminated.
Because I am cross compiling and the library is present on the Raspberry Pi I don't link the library.
Edit 2 to answer #Bartłomiej :
The following lines are located in my rpi-toolchain.cmake file which is used when invoking cmake with the following : -DCMAKE_TOOLCHAIN_FILE=rpi-toolchain.cmake. When I try to specify the sysroot by setting CMAKE_SYSROOT the compiler is not even able to compile a test file.
# Define our host system
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_SYSTEM_VERSION 1)
SET(rpi-tools-dir ${CMAKE_SOURCE_DIR}/rpi-tools)
# Define the C cross compiler location
SET(CMAKE_C_COMPILER ${rpi-tools-dir}/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc)
# Define the CXX cross compiler location
SET(CMAKE_CXX_COMPILER ${rpi-tools-dir}/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++)
# Define the sysroot path for the RaspberryPi distribution in our tools folder
SET(CMAKE_FIND_ROOT_PATH ${rpi-tools-dir}/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/sysroot/)
# Only use binaries from the host and not from the toolchain sysroot
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for libraries only in the target sysroot
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
# Search for the headers in the target and host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)

Related

CMake stdlib include issue

I need to compile a C++ project using CMake and MinGW-w64. The project depends on zlib, so CMakeLists.txt contains:
find_package(ZLIB REQUIRED)
CMake will fail on ZLIB unless I add the following path to CMAKE_PREFIX_PATH:
C:/Dev/mingw64-8.1.0/x86_64-w64-mingw32/include
With this path added, CMake runs fine. But, I get the following compile error:
#include_next <stdlib.h>
^~~~~~~~~~
C:/Dev/mingw64-8.1.0/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/cstdlib:75:15:
fatal error: stdlib.h: No such file or directory
After some search about this last error, I found I can fix it by editing the CMake includes_CXX.rsp and either:
Remove -isystem C:/Dev/mingw64-8.1.0/x86_64-w64-mingw32/include
Or change -isystem to -I
So basically I have to pass the include path to CMake so that it finds zlib.h, and remove this path from the files generated by CMake... Is there a proper solution?
Thanks!

CMake and Make need to be run twice in order to build code successfully

I am using CMake 3.8.2, GNU make 4.2.1 and GCC 6.4.0 for my C++14 project and I noticed a strange behavior when building. I am using CMake for an out-of-source build in a sub-folder called "build" where I run cmake .. followed by make.
CMake runs fine without any errors and make will build all source files like I expect until it is done compiling and starts linking them. It will then fail with an error
[ 83%] ...
[100%] Linking CXX executable myproject
/usr/bin/ld: some-source-file.cc.o: undefined reference to symbol '_ZNKSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEE3strEv##GLIBCXX_3.4.21'
Interestingly it doesn't show any compiler warnings up to this point and only shows the above mentioned linker error.
Now when I ignore the error and simply run cmake .. and then make again (just like I did before) I get all the compiler warnings that my code should produce and everything links perfectly fine, even though I didn't change any code or CMake-related files in the meantime.
I can reproduce this behavior by deleting all files in the build dir by running rm -r *.
Here is my CMakeLists.txt file:
# Define minimum required CMake version
cmake_minimum_required(VERSION 3.8.2)
# Setting compiler related settings
set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/toolchain/binary/gcc-6.4.0/bin/gcc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion -O2 -lstdc++")
set(CMAKE_CXX_STANDARD 14)
# Define project name
project(MyProject)
# Find source files
file(GLOB_RECURSE SOURCES application/*.cc)
# Adding third-party sources
set(SOURCES ${SOURCES} "third-party/cpp-base64/base64.cpp")
# Executable to be built from which source files
add_executable(myproject ${SOURCES})
# Find and include and link Botan
find_library(BOTAN botan-2 "third-party/botan/build/lib")
include_directories("third-party/botan/build/include/botan-2")
# Includes that are part of the project
include_directories("${CMAKE_SOURCE_DIR}/application/include")
# Include nlohmann/json
include_directories("third-party/json/src")
# Include cpp-base64 by René Nyffenegger
include_directories("third-party/cpp-base64")
find_package(Boost REQUIRED COMPONENTS program_options)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
endif()
# Link third-party libraries
target_link_libraries(myproject ${Boost_LIBRARIES} ${BOTAN})
Note: I am required to check-in the compiler and libraries I am using, which is why I specified them in the CMake file.
If it only works the second time it has to do with cached variables.
So I'm pretty sure that it will work the first time if you modify CMAKE_CXX_COMPILER setting by adding set(... CACHE INTERNAL "") to:
set(CMAKE_CXX_COMPILER "${CMAKE_SOURCE_DIR}/toolchain/binary/gcc-6.4.0/bin/gcc" CACHE INTERNAL "")
And move set(CMAKE_CXX_FLAGS ...) after the project() command.
But please also be noted that you shouldn't put the compiler into your CMakeLists.txt.
References
CMake: In which Order are Files parsed (Cache, Toolchain, …)?
Passing compiler options cmake
CMake Error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found

Cross-compile with CMake: How to exclude default qt libraries and not to pass them to linker's command line

I want to cross-compile Qt planets demo applicatioin on Linux x64 with CMake for ARM.
I installed arm cross compiler. I've built Qt libs on ARM board and copied them to x64 Linux box's /lib/arm-linux-gnueabihf.
For qmake I did this with only adding few lines to planets.pro file:
PLATFORM = arm-linux-gnueabihf
QMAKE_CXX = /usr/bin/$$PLATFORM-g++
QMAKE_LINK = /usr/bin/$$PLATFORM-g++
QMAKE_LIBS = -L/usr/$$PLATFORM/lib -L/lib/$$PLATFORM
It compiles successfully and runs perfectly on ARM board.
Now, I want to do this with CMake. So, I created the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
set(PLATFORM arm-linux-gnueabihf)
set(CMAKE_CXX_COMPILER /usr/bin/${PLATFORM}-g++)
link_directories(/usr/${PLATFORM}/lib /lib/${PLATFORM})
file(GLOB_RECURSE HEADERS "*.hpp")
file(GLOB SOURCES "*.cpp")
set(CMAKE_AUTOMOC ON)
find_package(Qt5Qml REQUIRED)
find_package(Qt5Quick REQUIRED)
add_executable(planets-qml ${SOURCES})
target_link_libraries(planets-qml
Qt5::Qml
Qt5::Quick
)
With this I have the following warning in Qt Creator's log:
Cannot generate a safe runtime search path for target planets-qml because
files in some directories may conflict with libraries in implicit
directories:
runtime library [libGL.so.1] in /usr/lib/x86_64-linux-gnu may be hidden by files in:
/lib/arm-linux-gnueabihf
Then, I'm building it, and I got linker error:
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Quick.so.5.7.0:
file not recognized: File format not recognized
I assume this happens because linker tries to link x64 Qt libraries to ARM binary.
But why can't it find libs from /lib/arm-linux-gnueabihf ?
I found the command to the linker in the file linker.txt:
/usr/bin/arm-linux-gnueabihf-g++
CMakeFiles/planets-qml.dir/main.cpp.o
CMakeFiles/planets-qml.dir/planets-qml_automoc.cpp.o
-o planets-qml
-L/usr/arm-linux-gnueabihf/lib -L/lib/arm-linux-gnueabihf
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Quick.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Qml.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Network.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Gui.so.5.7.0
/home/dev/Qt5.7.0/5.7/gcc_64/lib/libQt5Core.so.5.7.0
-Wl,-rpath,/usr/arm-linux-gnueabihf/lib:/lib/arm-linux-gnueabihf:/home/dev/Qt5.7.0/5.7/gcc_64/lib
So, the question is: How to exclude these /home/dev/Qt5... entries and not to pass them to the linker?
What CMake command should I use in CMakeLists.txt file?
Problem is that you are looking at the wrong QT directory.
You need to recompile Qt for ARM. Then set Qt5_DIR before find_package calls.
set(Qt5_DIR <path-to-Qt>/lib/cmake/Qt5/)
Your <path-to-Qt>/lib/cmake/Qt5/ should look like this:
$ ls <path-to-Qt>/lib/cmake/Qt5/
Qt5Config.cmake Qt5ConfigVersion.cmake

CMake cross compile

I'm having problem with my cmake cross compiler project.
My cross compiler did not find used libraries. I setup my cross compiler with this tutorial Cross Compiler.
Now i need libs they are installed on my RaspberryPi. I have snychronised my /lib and /usr direcotry from Pi to my Computer in /opt/cross/rasp. This is my Toolchain file:
# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)
# specify the cross compiler
SET(CMAKE_C_COMPILER
/opt/cross/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc)
SET(CMAKE_CXX_COMPILER
/opt/cross/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-g++)
# where is the target environment
SET(CMAKE_FIND_ROOT_PATH /opt/cross/rasp)
# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
But when i try to compile my program i get following Linking Error:
/opt/cross/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.9.1/../../../../arm-unknown-linux-gnueabi/bin/ld: warning: libltdl.so.7, needed by /opt/cross/rasp/usr/local/lib/libgphoto2.so, not found (try using -rpath or -rpath-link)
/opt/cross/x-tools/arm-unknown-linux-gnueabi/lib/gcc/arm-unknown-linux-gnueabi/4.9.1/../../../../arm-unknown-linux-gnueabi/bin/ld: warning: libexif.so.12, needed by /opt/cross/rasp/usr/local/lib/libgphoto2.so, not found (try using -rpath or -rpath-link)
On my RaspberrPi is compiling without errors possible.
The problem seems link to a misinterpretation of etc/ld.so.conf see fixing-rpath-link-issues-with-cross-compilers
In order to add the missing rpath-link, you can append to the toolchain cmake file :
SET(CMAKE_EXE_LINKER_FLAGS "-Wl,-rpath-link=/opt/cross/rasp/lib/arm-linux-gnueabihf:/opt/cross/rasp/usr/lib/arm-linux-gnueabihf")
I believe your cross-compile linker needs a --sysroot change. In the toolchain file you can use:
set(CMAKE_SYSROOT "/opt/cross/rasp")
Should anyone be looking for another reference, aside from the original question, I recommend reading through the ARM cross-compile guide I wrote for Takeoff Technical:
https://takeofftechnical.com/x-compile-cpp-bbb

cmake on macosx maverick, how to force /usr/include to be included

I am working on a macosx 10.9 with a fresh update of xcode and I have done xcode-select --install.
I have a C++ file that use the header GL/glew.h and stdio.h
#include <stdio.h>
#include <GL/glew.h>
Glew is installed in /usr/include.
$ ls /usr/include/GL
glew.h glxew.h wglew.h
stdio.h is also in the /usr/include but a locate shows that it is present in many folders including :
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/usr/include/
If I try to compile the code without specifying the /usr/local include path, I get
fatal error: 'GL/glew.h' file not found
#include <GL/glew.h>
Notice that stdio seems to have been included successfully.
If I add -I/usr/include to g++ in the command line the compilation is successful.
Now, I have a cMakeList.txt that add /usr/include, like that :
include_directories ( /usr/include/)
but in the resulting makefile the -I/usr/include/ does not appears and the compilation failed just as before.
My current workaround is a symbolic link to /usr/include/ named /usr/include2, and then include that directory. With that configuration the -I/usr/include2 is added to the makefile and everything works well. However it is ugly and I am missing something there.
My guess is that /usr/include should be included by default thus cmake skips it, however x-code use a different default include path (where incidentally stdio.h is also found).
Does anyone knows how to either :
fix xcode/g++ so that it (also) use by default /usr/include
force cmake to literally include /usr/include
I hope I have included enough information because it is my first question.
I'm not sure but I think cmake or xcode change their behaviour and now include directories automatically substitute from /usr/include/ to /.../Xcode.app/.../SDKs/MacOSX/usr/include
(it's working earlier for me). As a workaround you can add compiler flags explicitly:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/include")
note that in your case you can use find_package:
find_package(GLEW REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${GLEW_INCLUDE_DIRS}")
In XCode you should be able to add /usr/include to your search paths in your projects Build Settings: