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!
Related
I'm importing admesh library to my cmake project, I've followed the INSTALL instructions from the file in root directory of admesh and it was copied in usr/local/include / usr/local/lib. So, I've added this in my CMakeList:
find_path(ADMESH_INCLUDE_DIR stl.h HINTS "/usr/local/include/admesh")
FIND_LIBRARY(ADMESH_LIBRARY NAMES admesh)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ADMESH DEFAULT_MSG ADMESH_LIBRARY ADMESH_INCLUDE_DIR)
IF(ADMESH_FOUND)
message("admesh found")
SET( ADMESH_LIBRARIES ${ADMESH_LIBRARY} )
ENDIF(ADMESH_FOUND)
include_directories(${ADMESH_INCLUDE_DIR})
target_link_libraries(project PRIVATE admesh ${ADMESH_LIBRARIES})
but when I tried to build it give me this error:
fatal error: 'admesh/stl.h' file not found
#include <admesh/stl.h>
^~~~~~~~~~~~~~
1 error generated.
It prints admesh found so I think that there is something wrong in my CMakeList. How can I fix it?
Should be
find_path(ADMESH_INCLUDE_DIR admesh/stl.h HINTS "/usr/local/include")
or
find_path(ADMESH_INCLUDE_DIR admesh HINTS "/usr/local/include")
Usually /usr/local/include is in the system search include paths, thus all lines until arget_link_libraries(project PRIVATE admesh) can be removed.
I am porting a Windows project to CentOS Linux that uses cpprestsdk. I use vcpkg on Windows and I thought I would use vcpkg (and cmake) to bring in and build the packages and 'expose' the libs and header files to my project. The sequence fails in trying to get package header file 'known' to my source. This is what I did.
$ vcpkg install boost cpprestsdk
$ vcpkg integrate install
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=/vcpkg/scripts/buildsystems/vcpkg.cmake"
$ cd <source>
$ vi CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project(Domain)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -I../ ")
file(GLOB SOURCES "*.cpp")
#Generate the shared library from the sources
add_library(Domain SHARED ${SOURCES})
install(TARGETS Domain DESTINATION ../lib)
$ cmake -DCMAKE_TOOLCHAIN_FILE=/vcpkg/scripts/buildsystems/vcpkg.cmake -G "Unix Makefiles" .
$ make
[ 7%] Building CXX object CMakeFiles/Domain.dir/BaseDataFactory.cpp.o
In file included from /src/Domain/stdafx.h:4:0,
from /src/Domain/BaseDataFactory.cpp:1:
../Common/Common.h:75:26: fatal error: cpprest/json.h: No such file or directory
#include <cpprest/json.h>
By adding the vcpkg toolchain file parameter to cmake, I thought it was supposed to take care of exposing all of the package paths (lib/header) and write them to the output Makefile?
I tried adding
find_package(cpprestsdk REQUIRED)
Then I got bunch of new errors:
CMake Error at CMakeLists.txt:7 (find_package):
Could not find a package configuration file provided by "cpprestsdk" with
any of the following names:
cpprestsdkConfig.cmake
cpprestsdk-config.cmake
cpprestConfig.cmake
cpprest-config.cmake
cpprestsdk-config.cmake does exist under the vcpkg root directory and I can definitely see the offending header file for the package under the vcpkg root directory, but why does the cmake-generated Makefile not have everything it needs to build? Does each and every package under vcpkg have to be manually included in some way in the CMakeLists.txt file?
I found the problem. Cpprestsdk does not register/expose any cmake find_package() config module. If it did that, this wouldn't be an issue - the generated toolchain file would set everything cmake needs in order to generate the paths to include in the MakeFile.
I added the following line to the CMakeList.txt file and cmake was then able to find the config file:
set(cpprestsdk_DIR "/vcpkg/installed/x64-linux/share/cpprestsdk")
Which is really, really bad, IMHO, to have to hard code a path to find cpprestsdk. I still have the header file path problem, so there's actually much more going wrong/missing. I'll update this post once I get word from the vcpkg/cmake teams.
https://github.com/Microsoft/cpprestsdk/blob/ea4eff7cd1d6110833df869f7591f266816f8328/Release/src/CMakeLists.txt#L282-L285
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/cpprestsdk-config.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${CPPREST_EXPORT_DIR}
)
and
https://github.com/Microsoft/cpprestsdk/blob/9d8f544001cb74544de6dc8c565592f7e2626d6e/Release/CMakeLists.txt#L22
set(CPPREST_EXPORT_DIR cpprestsdk CACHE STRING "Directory to install CMake config files.")
One thing puzzle me (not a vcpkg expert yet)
https://github.com/Microsoft/vcpkg/blob/master/ports/cpprestsdk/portfile.cmake#L45
vcpkg_fixup_cmake_targets(CONFIG_PATH lib/share/cpprestsdk)
while it should be lib/cpprestsdk IMHO (ed: not checked SHA1 version)
#cardinalPilot did you try to locate the config file on your system ?
can you also try to print the CMAKE_PREFIX_PATH etc to see if vcpkg do correctly its stuff...
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)
I'm working on a Cmake based C++ project in QtCreator. I am trying to use mlpack in my project, but as soon as I include the line #include <mlpack/core.hpp> to my only source file, I get an error pertaining to some libxml files that are included by mlpack/core.hpp:
In file included from /usr/local/include/mlpack/core/util/save_restore_utility.hpp:26:0,
from /usr/local/include/mlpack/core.hpp:171,
from /home/revinci/code/workspaces/qt_ws/Semantic_Perception/src/features_slic.cpp:18:
/usr/include/libxml2/libxml/parser.h:15:31: fatal error: libxml/xmlversion.h: No such file or directory
#include <libxml/xmlversion.h>
Now, I went into /usr/include/libxml2/libxml/ and found parser.h with the line #include <libxml/xmlversion.h> in it.
So, I saw that xmlversion.h and parser.h are in the same folder and tried a hack: I changed the #include <libxml/xmlversion.h> in parser.h to #include "xmlversion.h" only to get the following error:
In file included from /usr/include/libxml2/libxml/parser.h:15:0,
from /usr/local/include/mlpack/core/util/save_restore_utility.hpp:26,
from /usr/local/include/mlpack/core.hpp:171,
from /home/revinci/code/workspaces/qt_ws/Semantic_Perception/src/features_slic.cpp:18:
/usr/include/libxml2/libxml/xmlversion.h:13:31: fatal error: libxml/xmlexports.h: No such file or directory
#include <libxml/xmlexports.h>
Which is basically telling me that it can't find xmlexports.h (included by xmlversion.h). More importantly, xmlexports.h is in the same directory as xmlversion.h and parser.h!
I tried the solution mentioned here and installed libxml2-dev (again) and libxslt1-dev, but my problem wasn't solved.
I think this may have something to do with specifying my include paths correctly. I've tried to add /usr/include/libxml2 to the various path environment variables (PATH, INCLUDE_PATH and CPATH) that are present in my build environment in QtCreator, but to no avail. My CMakeLists.txt looks like this:
project(Semantic_Perception)
cmake_minimum_required(VERSION 2.8)
#Vigra Headers
include_directories(
include
)
file(GLOB_RECURSE VigraImpex include/impex/)
add_library(VigraImpex ${VigraImpex})
#LibXml2 Headers
find_package(LibXml2 REQUIRED)
#Armadillo Headedrs
find_package(Armadillo REQUIRED)
include_directories(${ARMADILLO_INCLUDE_DIRS})
#Boost Headers
find_package(Boost 1.54.0 REQUIRED)
add_executable(features_slic src/features_slic.cpp)
target_link_libraries(features_slic
VigraImpex
${ARMADILLO_LIBRARIES}
)
BTW: LibXml2, Armadillo and Boost are all dependencies of the library I am trying to use - mlpack. The command find_pakcage(mlpack) won't work because there is no Findmlpack.cmake file on my system anywhere, and I couldn't find one on the internet either.
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: