Fail to run the example of 3D Surface Mesh Generation with CGAL - c++

I am trying to run this example of 3D Surface Mesh Generation with CGAL. The code is:
#include <CGAL/Surface_mesh_default_triangulation_3.h>
#include <CGAL/Complex_2_in_triangulation_3.h>
#include <CGAL/make_surface_mesh.h>
#include <CGAL/Implicit_surface_3.h>
// default triangulation for Surface_mesher
typedef CGAL::Surface_mesh_default_triangulation_3 Tr;
// c2t3
typedef CGAL::Complex_2_in_triangulation_3<Tr> C2t3;
typedef Tr::Geom_traits GT;
typedef GT::Sphere_3 Sphere_3;
typedef GT::Point_3 Point_3;
typedef GT::FT FT;
typedef FT (*Function)(Point_3);
typedef CGAL::Implicit_surface_3<GT, Function> Surface_3;
FT sphere_function (Point_3 p) {
const FT x2=p.x()*p.x(), y2=p.y()*p.y(), z2=p.z()*p.z();
return x2+y2+z2-1;
}
int main() {
Tr tr; // 3D-Delaunay triangulation
C2t3 c2t3 (tr); // 2D-complex in 3D-Delaunay triangulation
// defining the surface
Surface_3 surface(sphere_function, // pointer to function
Sphere_3(CGAL::ORIGIN, 2.)); // bounding sphere
// Note that "2." above is the *squared* radius of the bounding sphere!
// defining meshing criteria
CGAL::Surface_mesh_default_criteria_3<Tr> criteria(30., // angular bound
0.1, // radius bound
0.1); // distance bound
// meshing surface
CGAL::make_surface_mesh(c2t3, surface, criteria, CGAL::Non_manifold_tag());
std::cout << "Final number of points: " << tr.number_of_vertices() << "\n";
}
The CMakeLists file contains:
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
cmake_minimum_required(VERSION 3.7)
project(_cgal)
# CGAL and its components
find_package( CGAL QUIET COMPONENTS Core )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# include helper file
include( ${CGAL_USE_FILE} )
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# Creating entries for target: out
# ############################
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(_cgal ${SOURCE_FILES})
add_to_cached_list( CGAL_EXECUTABLE_TARGETS _cgal )
# Link the executable to CGAL and third-party libraries
target_link_libraries(_cgal ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} )
Output of cmake:
-- The C compiler identification is GNU 7.1.0
-- The CXX compiler identification is GNU 7.1.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/local/bin/gcc-7
-- Check for working C compiler: /usr/local/bin/gcc-7 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /usr/local/bin/g++-7
-- Check for working CXX compiler: /usr/local/bin/g++-7 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Build type: Release
-- USING CXXFLAGS = ' -DNDEBUG'
-- USING EXEFLAGS = ' '
-- Targetting Unix Makefiles
-- Using /usr/local/bin/g++-7 compiler.
-- DARWIN_VERSION=16
-- Mac Leopard detected
-- Requested component: Core
-- Requested component: MPFR
-- Requested component: GMP
-- Boost version: 1.64.0
-- Configuring done
-- Generating done
However, I am getting the following error:
Scanning dependencies of target _cgal
[ 50%] Building CXX object CMakeFiles/_cgal.dir/main.cpp.o
[100%] Linking CXX executable _cgal
Undefined symbols for architecture x86_64:
"CGAL::get_mode(std::basic_ios<char, std::char_traits<char> >&)", referenced from:
std::basic_ostream<char, std::char_traits<char> >& CGAL::insert<CGAL::Epick>(std::basic_ostream<char, std::char_traits<char> >&, CGAL::Point_3<CGAL::Epick> const&, CGAL::Cartesian_tag const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[3]: *** [_cgal] Error 1
make[2]: *** [CMakeFiles/_cgal.dir/all] Error 2
make[1]: *** [CMakeFiles/_cgal.dir/rule] Error 2
make: *** [_cgal] Error 2
I would highly appreciate if you could explain me how to fix it.
UPD: This seems to be related, but do not get what packages I should add in my cmake.
UPD2: I found that it may be related to the issue of:
There are two implementations of the standard C++ library available on
OS X: libstdc++ and libc++. They are not binary compatible and libMLi3
requires libstdc++.
On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is
chosen by default. To ensure compatibility with libMLi3, we need to
choose libstdc++ manually.
To do this, add -stdlib=libstdc++ to the linking command.
I modified the following line in the CMake file, but it did not help:
# Link the executable to CGAL and third-party libraries
target_link_libraries(_cgal ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} -static-libstdc++)
UPD3: I tried the solution suggested here, however it does not help
project( _cgal )
cmake_minimum_required(VERSION 2.8.10)
find_package(CGAL QUIET COMPONENTS Core )
if ( CGAL_FOUND )
include( ${CGAL_USE_FILE} )
include( CGAL_CreateSingleSourceCGALProgram )
create_single_source_cgal_program( "main.cpp" )
else()
message(STATUS "This program requires the CGAL library, and will not be compiled.")
endif()

The answer is: fix compiler! CGAL was installed with brew that uses clang compiler. But to compile the project I had been using g++. I should code in C++ more often :)

Related

CMake finds Boost, but fails to link

I am trying to do the tutorial on the boost website (https://www.boost.org/doc/libs/1_81_0/doc/html/program_options/tutorial.html), but I cannot get cmake to link to boost. Right now I am only trying to link to program_options. I have pulled boost from github and built it.
intensity.cpp
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int ac, char *av[]){
po::options_description desc("allowed options");
desc.add_options()
("help", "--intesity: set intesity levels")
("intensity", po::value<int>(), "set intensity level")
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if(vm.count("help")) {
std::cout << desc << "\n";
return 1;
}
if(vm.count("intensity")) {
std::cout << "intensity level set to "
<< vm["intensity"].as<int>() << "\n";
} else {
std::cout << "intesity was not set\n";
}
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(Intensity VERSION 1.0)
set(CMAKE_CXX_COMPILER clang)
set(BOOST_ROOT "/Users/nabiel.kandiel/code/thirdparty/boost_1_81_0")
set(Boost_USE_STATIC_LIBS OFF)
set(BOOST_USE_MULTITHREADED ON)
set(BOOST_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.81 REQUIRED COMPONENTS program_options)
add_executable(intensity intensity.cpp)
target_link_libraries(intensity Boost::program_options)
cmake output
NKandiel-REMM22:build nabiel.kandiel$ cmake ../
-- The C compiler identification is AppleClang 14.0.0.14000029
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /opt/homebrew/lib/cmake/Boost-1.81.0/BoostConfig.cmake (found suitable version "1.81.0", minimum required is "1.81") found components: program_options
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nabiel.kandiel/code/imageProc/build
cmake build
NKandiel-REMM22:build nabiel.kandiel$ cmake --build .
[ 50%] Building CXX object CMakeFiles/intensity.dir/intensity.cpp.o
[100%] Linking CXX executable intensity
Undefined symbols for architecture arm64:
"std::logic_error::what() const", referenced from:
vtable for boost::program_options::error in intensity.cpp.o
"std::runtime_error::what() const", referenced from:
vtable for boost::wrapexcept<boost::bad_function_call> in intensity.cpp.o
vtable for boost::bad_function_call in intensity.cpp.o
"std::__1::__vector_base_common<true>::__throw_length_error() const", referenced
.....
std::__1::allocator<boost::program_options::basic_option<char> >::deallocate(boost::program_options::basic_option<char>*, unsigned long) in intensity.cpp.o
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [intensity] Error 1
make[1]: *** [CMakeFiles/intensity.dir/all] Error 2
make: *** [all] Error 2
I have tried using a combination of CMakeLists.txt I have found online and none have worked.

Cross-compiling enet from Linux to Windows, linking errors

Introduction
I'm trying to cross compile to Windows from Linux using MinGW-W64.
I had it working before I added enet to my project, however I'm now receiving issues with linking to enet
CMake finds enet correctly. ENET_LIBRARY and ENET_INCLUDE_DIR are set to the right locations.
ENet contains the symbols, as verified using /usr/x86_64-w64-mingw32/bin/objdump /usr/local/mingw64/lib/libenet.a -t
Build fails with "undefined reference to `enet_address_set_host'"
I'm able to compile the same code base natively using Visual Studio and VCPkg
Edit: Checking the contents of libenet.a verifies that it's a problem with cross-compiling enet, not my program in particular
None of my other dependencies use GNU autoconf, so I expect there's a problem there.
The Error
-- The C compiler identification is GNU 8.2.0
-- The CXX compiler identification is GNU 8.2.0
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /usr/bin/x86_64-w64-mingw32-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SFML 2.5 in /usr/local/mingw64/include
-- Found SFGUI in /usr/local/mingw64/include
-- Found Thor in /usr/local/mingw64/include
-- Found Lua in /usr/local/mingw64/include/lua5.1/
-- Found Lua: /usr/local/mingw64/lib/liblua5.1.a
-- Found ENet: /usr/local/mingw64/lib/libenet.a
-- Found ENet in /usr/local/mingw64/include
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - found
-- Found Threads: TRUE
/usr/local/mingw64/lib/liblua5.1.a/usr/local/mingw64/lib/libenet.awsock32ws2_32winmm
-- Adding executable: client (with server)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ruben/dev/rvwp
Scanning dependencies of target rvwp
[SNIP]
[ 98%] Building CXX object CMakeFiles/rvwp.dir/source/tests/t_chunk.cpp.obj
[100%] Linking CXX executable bin/rvwp.exe
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(address.cpp.obj):address.cpp:(.text+0xc6): undefined reference to `enet_address_set_host'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x105): undefined reference to `enet_initialize'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x204): undefined reference to `enet_packet_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x217): undefined reference to `enet_peer_send'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x29f): undefined reference to `enet_packet_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x2b3): undefined reference to `enet_peer_send'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x5af): undefined reference to `enet_host_destroy'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x65b): undefined reference to `enet_host_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x91f): undefined reference to `enet_host_create'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x950): undefined reference to `enet_host_connect'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x96c): undefined reference to `enet_host_service'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x1023): undefined reference to `enet_host_service'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x1437): undefined reference to `enet_packet_destroy'
/usr/lib/gcc/x86_64-w64-mingw32/8.2.0/../../../../x86_64-w64-mingw32/bin/ld: CMakeFiles/rvwp.dir/objects.a(net.cpp.obj):net.cpp:(.text+0x121): undefined reference to `enet_deinitialize'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/rvwp.dir/build.make:991: bin/rvwp.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/rvwp.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
Cross-compiling Enet
To cross compile enet, I use the following script
#!/bin/bash
TOOLSET="x86_64-w64-mingw32"
wget http://enet.bespin.org/download/enet-1.3.13.tar.gz
tar -xzf enet-1.3.13.tar.gz
cd enet-1.3.13
./configure \
--build=${TOOLSET} \
--host=x86_64-windows \
--target=${TOOLSET} \
--prefix=/usr/local/mingw64 \
--enable-shared
make -j3
sudo make install
My Program
I'm using cmake to generate the makefiles, and a toolchain to allow cross-compilation. The program compiles with SFML, thor, std::thread, and Lua fine. None of these libraries use GNU autoconf
My CMakeLists.txt looks like this:
find_package(ENet REQUIRED)
include_directories(${ENET_INCLUDE_DIR})
set(BASE_LIBRARIES ${CMAKE_THREAD_LIBS_INIT} ${LUA_LIBRARY} ${CMAKE_DL_LIBS} ${ENET_LIBRARY})
set(BASE_LIBRARIES ${BASE_LIBRARIES} wsock32 ws2_32 winmm)
message(${BASE_LIBRARIES})
set(EXECUTABLE_NAME "rvwp")
add_executable(${EXECUTABLE_NAME} WIN32 ${CLIENT_SRC})
set_target_properties(${EXECUTABLE_NAME} PROPERTIES BUILD_WITH_INSTALL_RPATH true)
install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)
target_link_libraries(${EXECUTABLE_NAME} ${BASE_LIBRARIES} ${SFML_LIBRARIES} ${SFGUI_LIBRARY} ${THOR_LIBRARY})
The findENet file looks like this:
FIND_PATH(ENET_INCLUDE_DIR enet/enet.h
PATHS
$ENV{ENETDIR}
/usr/local
/usr
PATH_SUFFIXES include)
FIND_LIBRARY(ENET_LIBRARY
NAMES enet
PATHS
$ENV{ENETDIR}
/usr/local
/usr
PATH_SUFFIXES lib)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ENet DEFAULT_MSG ENET_LIBRARY ENET_INCLUDE_DIR)
IF (ENet_FOUND)
MESSAGE(STATUS "Found ENet in ${ENET_INCLUDE_DIR}")
IF(WIN32)
SET(WINDOWS_ENET_DEPENDENCIES "ws2_32;winmm")
SET(ENET_LIBRARIES ${ENET_LIBRARY} ${WINDOWS_ENET_DEPENDENCIES})
ELSE(WIN32)
SET(ENET_LIBRARIES ${ENET_LIBRARY})
ENDIF(WIN32)
ENDIF (ENet_FOUND)
MARK_AS_ADVANCED(ENET_LIBRARY ENET_LIBRARIES ENET_INCLUDE_DIR)
The tool chain looks like this:
# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
# *) install cross compiler: `sudo apt-get install mingw-w64 g++-mingw-w64`
# *) cd build
# *) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-Ubuntu-mingw64.cmake ..
set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++" )
# target environment on the build host system
# set 1st to dir with the cross compiler's C/C++ headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /usr/local/mingw64 ./extlibs)
set(LUA_INCLUDE_DIR /usr/local/mingw64/include/lua5.1/)
set(LUA_LIBRARY /usr/local/mingw64/lib/liblua5.1.a)
set(OPENAL_LIBRARY /usr/local/mingw64/lib/libopenal32.a)
# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
The linklibs.rsp file is used to pass linking commands to the linker, here is its value:
/usr/local/mingw64/lib/liblua5.1.a /usr/local/mingw64/lib/libenet.a -lwsock32 -lws2_32 -lwinmm /usr/local/mingw64/lib/libsfml-system.a /usr/local/mingw64/lib/libsfml-window.a /usr/local/mingw64/lib/libsfml-graphics.a /usr/local/mingw64/lib/libsfml-network.a /usr/local/mingw64/lib/libsfml-audio.a /usr/local/mingw64/bin/sfgui.dll /usr/local/mingw64/bin/libthor.dll -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
(EDIT) AR Formats
Extracting the not-working libenet.a and using file results in this:
callbacks.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), with debug_info, not stripped
Extracting the working libsfml-graphics.a results in this:
d000508.o: Intel amd64 COFF object file, no line number info, not stripped, 5 sections, symbol offset=0x144, 8 symbols
So it appears that the problem is in compiling enet

make openmp g++-6.2.0 no such file [duplicate]

This question already has an answer here:
CMake - set_property could not find CACHE variable
(1 answer)
Closed 6 years ago.
cmakelist.txt
cmake_minimum_required(VERSION 3.7)
project(multithreading)
# Find ITK.
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_C_FLAGS “${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}”)
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}“)
set(CMAKE_EXE_LINKER_FLAGS “${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}”)
endif()
add_executable(multithreading multithreading.cpp )
target_link_libraries(multithreading ${ITK_LIBRARIES})
TARGET_LINK_LIBRARIES(multithreading ${OpenMP_CXX_LIBRARIES})
And then I change default compiler which is AppleClang to gcc-6.2.0, and then cmake
,all openmp flags find, and configure is fine.
cmake -DCMAKE_C_COMPILER=/usr/local/gcc-6.2.0/bin/gcc-6.2.0 -DCMAKE_CXX_COMPILER=/usr/local/gcc-6.2.0/bin/g++-6.2.0 ./
-- The C compiler identification is GNU 6.2.0
-- The CXX compiler identification is GNU 6.2.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/local/gcc-6.2.0/bin/gcc-6.2.0
-- Check for working C compiler: /usr/local/gcc-6.2.0/bin/gcc-6.2.0 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /usr/local/gcc-6.2.0/bin/g++-6.2.0
-- Check for working CXX compiler: /usr/local/gcc-6.2.0/bin/g++-6.2.0 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Try OpenMP C flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Success
-- Try OpenMP CXX flag = [-fopenmp]
-- Performing Test OpenMP_FLAG_DETECTED
-- Performing Test OpenMP_FLAG_DETECTED - Success
-- Found OpenMP: -fopenmp
OPENMP FOUND
-- Configuring done
-- Generating done
then next when I do ‘make’ it shows me
canning dependencies of target multithreading
[ 50%] Building CXX object CMakeFiles/multithreading.dir/multithreading.cpp.o
g++-6.2.0: error: “: No such file or directory
g++-6.2.0: fatal error: no input files
compilation terminated.
/bin/sh: -fopenmp“: command not found
make[2]: *** [CMakeFiles/multithreading.dir/multithreading.cpp.o] Error 127
make[1]: *** [CMakeFiles/multithreading.dir/all] Error 2
make: *** [all] Error 2
If I do g++ -fopenmp myproject.cpp it will not link ITK Library
g++ -fopenmp multithreading.cpp
multithreading.cpp:4:44: fatal error: itkRescaleIntensityImageFilter.h: No such file or directory
#include "itkRescaleIntensityImageFilter.h"
^
compilation terminated.
So I am wondering how can I fix that issue, so use itk and openmp as library at the same time.
You're using language/locale-specific double quotes. Most programming languages don't support these, including CMake. Use the "normal" double quotes: "

cmake: why doesn't CMAKE_CXX_STANDARD seem to work with check_cxx_source_compiles

Here's an MCVE:
cmake_minimum_required(VERSION 3.1)
Project(Test)
include(CheckCXXSourceCompiles)
set (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
#set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
#set (CMAKE_CXX_STANDARD 11)
#set (CMAKE_CXX_EXTENSIONS FALSE)
check_cxx_source_compiles("
#include <atomic>
int main() {
std::atomic<int> u{5};
return u;
}" HAVE_STDLIB_ATOMIC)
if (NOT HAVE_STDLIB_ATOMIC)
message(FATAL_ERROR "Did not find std::atomic support!")
endif()
When I use the CMAKE_CXX_FLAGS version, it works fine, but when I use the new CMAKE_CXX_STANDARD flags which we are supposed to use now, it doesn't work, I get the following build errors:
$ cmake ..
-- The C compiler identification is GNU 5.4.1
-- The CXX compiler identification is GNU 5.4.1
-- 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test HAVE_STDLIB_ATOMIC
-- Performing Test HAVE_STDLIB_ATOMIC - Failed
CMake Error at CMakeLists.txt:20 (message):
Did not find std::atomic support!
-- Configuring incomplete, errors occurred!
See also "/home/chris/cmake_test/build/CMakeFiles/CMakeOutput.log".
See also "/home/chris/cmake_test/build/CMakeFiles/CMakeError.log".
The error log indicates it's not using the -std=c++11 flag:
$ cat /home/chris/cmake_test/build/CMakeFiles/CMakeError.log
Performing C++ SOURCE FILE Test HAVE_STDLIB_ATOMIC failed with the following output:
Change Dir: /home/chris/cmake_test/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_42a05/fast"
/usr/bin/make -f CMakeFiles/cmTC_42a05.dir/build.make CMakeFiles/cmTC_42a05.dir/build
make[1]: Entering directory '/home/chris/cmake_test/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_42a05.dir/src.cxx.o
/usr/bin/c++ -DHAVE_STDLIB_ATOMIC -o CMakeFiles/cmTC_42a05.dir/src.cxx.o -c /home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx
In file included from /usr/include/c++/5/atomic:38:0,
from /home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:2:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
^
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx: In function ‘int main()’:
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:5:3: error: ‘atomic’ is not a member of ‘std’
std::atomic<int> u{5};
^
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:5:15: error: expected primary-expression before ‘int’
std::atomic<int> u{5};
^
/home/chris/cmake_test/build/CMakeFiles/CMakeTmp/src.cxx:6:10: error: ‘u’ was not declared in this scope
return u;
^
CMakeFiles/cmTC_42a05.dir/build.make:65: recipe for target 'CMakeFiles/cmTC_42a05.dir/src.cxx.o' failed
make[1]: *** [CMakeFiles/cmTC_42a05.dir/src.cxx.o] Error 1
make[1]: Leaving directory '/home/chris/cmake_test/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_42a05/fast' failed
make: *** [cmTC_42a05/fast] Error 2
Source file was:
#include <atomic>
int main() {
std::atomic<int> u{5};
return u;
}
My cmake version is:
$ cmake --version
cmake version 3.5.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
I can't figure out why cmake doesn't use the std=c++11 flag here, according to the docu, CMAKE_CXX_STANDARD is supposed to work since version 3.1.
Anyone know what's wrong here?
What's the most appropriate workaround? Should I use CMAKE_CXX_FLAGS adjustments for the tests, and CMAKE_CXX_STANDARD for the targets? It seems to me that the exact same configuration should be used for the tests and the targets, otherwise what's the point of the tests :/
According to documentation, you can set CMAKE_REQUIRED_FLAGS to make try_compile use C++11 flag.
Since CMake 3.8, you can add
cmake_policy(SET CMP0067 NEW)
(you do need CMAKE_CXX_STANDARD_REQUIRED=ON). See the CMake doc on CMP0067

How can I make a library find Eigen with CMake in macOS?

I am trying to compile my project with CMake which includes the Ceres Solver library. I'm using macOS Sierra with Xcode 8.1 dev tools.
I installed the library with Homebrew (brew install ceres-solver). I downloaded and tested the binary manually (http://ceres-solver.org/building.html#mac-os-x), and that works just fine. But I can't include it in my own project because it can't seem to find Eigen. Here is a complete example:
ceres-test/CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(CeresTest)
find_package(ceres REQUIRED)
add_executable(
TestCeres
src/test_ceres.cpp
)
target_link_libraries(
TestCeres
ceres
)
ceres-test/src/test_ceres.cpp
#include <iostream>
#include "ceres/ceres.h"
int main(int argc, char** argv) {
std::cout << "Works." << std::endl;
return 0;
}
How I compile it:
mkdir build
cd build
cmake ..
make
Full output:
me: ceres-test $ mkdir build
me: ceres-test $ cd build/
cmake
me: build $ cmake ..
-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- 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++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found required Ceres dependency: Eigen version 3.2.10 in /usr/local/include/eigen3
-- Found required Ceres dependency: Glog in /usr/local/include
-- Found Ceres version: 1.11.0 installed in: /usr/local
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/me/Tests/ceres-test/build
make
me: build $ make
Scanning dependencies of target TestCeres
[ 50%] Building CXX object CMakeFiles/TestCeres.dir/src/test_ceres.cpp.o
In file included from /Users/me/Tests/ceres-test/src/test_ceres.cpp:3:
In file included from /usr/local/include/ceres/ceres.h:37:
In file included from /usr/local/include/ceres/autodiff_cost_function.h:132:
In file included from /usr/local/include/ceres/internal/autodiff.h:145:
/usr/local/include/ceres/jet.h:165:10: fatal error: 'Eigen/Core' file not found
#include "Eigen/Core"
^
1 error generated.
make[2]: *** [CMakeFiles/TestCeres.dir/src/test_ceres.cpp.o] Error 1
make[1]: *** [CMakeFiles/TestCeres.dir/all] Error 2
make: *** [all] Error 2
I have no idea how to resolve this. None of the solutions I found online helped. CMake seems to be finding the Eigen library just fine, so I'm not sure how to add it in.
On a side note I cannot include "Eigen/Core" directly either, but the tests that I was able to compile do include it and those are fine. I'm not familiar how to deal with these kinds of problems with CMake.
Edit: I can get it to compile if I include it as "eigen3/Eigen/Core" but I can't change the source code for Ceres.
Fixed with
include_directories(${EIGEN_INCLUDE_DIR})
in the CMakeLists....
For my mac mini m1.
I found the eigen library through
brew link --overwrite eigen.
It's located on
/opt/homebrew/Cellar/eigen/3.4.0_1/include/eigen3