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

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

Related

CMake with clang shows undefined symbol, and with cl links correctly

TLDR
Im building a static library and linking it to an executable. Im generating makefiles with cmake. When I generate the makefile for cl (The compiler from visual studio) I have no problems, but when I generate the makefile for clang I get an undefined symbol.
Versions:
cmake version 3.18.1
clang version 11.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
cl version 19.26.28806 for x64
Minimal Example
I have the following structure for the proyect:
Proyect Root
│ CMakeLists.txt
│ foo.cpp
│ foo.hpp
│ main.cpp
│
└───bin
And this are the contents of the files:
foo.hpp
namespace foo {
void do_stuff(void);
}
foo.cpp
#include <foo.hpp>
namespace foo {
void do_stuff(void) {}
}
main.cpp
#include <foo.hpp>
int main(void) {
foo::do_stuff();
return 0;
}
And CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(CompileAndLinkLib)
include_directories(".")
file(GLOB FOO_SRC "foo.cpp")
add_library(foo STATIC ${FOO_SRC})
add_executable(main "main.cpp")
target_link_libraries(main foo)
Correct linking
First I call vcvarsall.bat. I generate a nmake file with the following command:
cmake .. -G "NMake Makefiles"
And compile with:
nmake
The project compiles and links correctly
The undefined symbol
I generate the make file with the following command:
cmake .. -G "Unix Makefiles" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
And when I compile with make I get the following output:
Scanning dependencies of target foo
[ 25%] Building CXX object CMakeFiles/foo.dir/foo.cpp.obj
[ 50%] Linking CXX static library foo.lib
[ 50%] Built target foo
Scanning dependencies of target main
[ 75%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
[100%] Linking CXX executable main.exe
lld-link: error: undefined symbol: void __cdecl foo::do_stuff(void)
>>> referenced by C:\Users\pabsa\temp\main.cpp:4
>>> CMakeFiles/main.dir/main.cpp.obj:(main)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [main.exe] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
Im not sure if im doing something wrong, like if im missing something, or if this is an error with the makefile generated by cmake.
I just had the same problem, but with Clang 13. It could be solved with specifying clang as compiler in the CMakeLists, instead of letting CMake determine it automatically.
set(CMAKE_CXX_COMPILER "clang++")
(And yes, I read your original question correct, using this:
-DCMAKE_CXX_COMPILER=clang++
as an additional argument for cmake did not work for me as well, even if it looks like it should behave the same as setting it in CMakeLists.txt)
I tested again with:
clang version 12.0.0
Target: x86_64-pc-windows-msvc
Thread model: posix
And it worked corretly. Seems like it was a problem with clang 11
You're overcomplicating things imo. What about just "cmake .. -T ClangCL && cmake --build ."? Builds fine for me:
C:\Users\vital\test\_build>cmake .. -T ClangCL
-- Building for: Visual Studio 17 2022
-- Selecting Windows SDK version 10.0.20348.0 to target Windows 10.0.22000.
-- The C compiler identification is Clang 14.0.5 with MSVC-like command-line
-- The CXX compiler identification is Clang 14.0.5 with MSVC-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe - 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: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/vital/test/_build
C:\Users\vital\test\_build>cmake --build .
MSBuild version 17.3.1+2badb37d1 for .NET Framework
Checking Build System
Building Custom Rule C:/Users/vital/test/CMakeLists.txt
foo.vcxproj -> C:\Users\vital\test\_build\Debug\foo.lib
Building Custom Rule C:/Users/vital/test/CMakeLists.txt
main.vcxproj -> C:\Users\vital\test\_build\Debug\main.exe
Building Custom Rule C:/Users/vital/test/CMakeLists.txt
C:\Users\vital\test\_build>

Error when using cmake on vk_chopper

So, I wanted to install Vulkan on ubuntu 16.10.
I decided to follow this guide
Unfortunately at step 9 get to the choppa, I got an error.
cmake .. did not work for me ; this is what happened
Log
ra141#ra141:~$ cd ~/vulkan
ra141#ra141:~/vulkan$ cd gl_vk_chopper
ra141#ra141:~/vulkan/gl_vk_chopper$ cd build
ra141#ra141:~/vulkan/gl_vk_chopper/build$ rm -rf ./*
ra141#ra141:~/vulkan/gl_vk_chopper/build$ export VK_SDK_PATH=/home/ra141/vulkan/VulkanSDK
ra141#ra141:~/vulkan/gl_vk_chopper/build$ cmake ..
-- The C compiler identification is GNU 6.2.0
-- The CXX compiler identification is GNU 6.2.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
-- 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
-- Processing Project gl_vk_chopper:
-- BASE_DIRECTORY = /home/ra141/vulkan/gl_vk_chopper/..
-- CMAKE_CURRENT_SOURCE_DIR = /home/ra141/vulkan/gl_vk_chopper
-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so
-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so - found
-- Looking for gethostbyname
-- Looking for gethostbyname - found
-- Looking for connect
-- Looking for connect - found
-- Looking for remove
-- Looking for remove - found
-- Looking for shmat
-- Looking for shmat - found
-- Found X11: /usr/lib/x86_64-linux-gnu/libX11.so
-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libGL.so
-- Found GLEW: /home/ra141/vulkan/shared_sources/glew/include
-- Could NOT find GLFW (missing: GLFW_INCLUDE_DIR GLFW_LIBRARY)
-- found Glew source code. Using it instead of library
-- VulkanSDK search paths:
-- VulkanSDK version:
CMake Warning at /home/ra141/vulkan/shared_sources/cmake/FindVulkanSDK.cmake:133 (message):
VULKANSDK not found.
either env. VK_SDK_PATH should be set directly to the right version to use (C:\VulkanSDK\1.0.1.1)
or you can specify in cmake VULKANSDK_LOCATION to the folder where VulkanSDK versions are put (C:\VulkanSDK)
Call Stack (most recent call first):
/home/ra141/vulkan/shared_sources/CMakeLists_include.txt:341 (find_package)
CMakeLists.txt:44 (_add_package_VulkanSDK)
-- Vulkan Root : VULKANSDK_ROOT_DIR-NOTFOUND
-- Vulkan include : VULKANSDK_INCLUDE_DIR-NOTFOUND
-- Vulkan Library : VULKAN_LIB-NOTFOUND
-- Could NOT find VULKANSDK (missing: VULKANSDK_INCLUDE_DIR VULKAN_LIB)
-- --> NOT using package VulkanSDK
-- --> using package AntTweakBar
-- Found ANTTWEAKBAR: /usr/include
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
X11_Xcursor_LIB (ADVANCED)
linked by target "gl_vk_chopper" in directory /home/ra141/vulkan/gl_vk_chopper
linked by target "gl_vk_chopper" in directory /home/ra141/vulkan/gl_vk_chopper
linked by target "shared_sources" in directory /home/ra141/vulkan/shared_sources
X11_Xinerama_LIB (ADVANCED)
linked by target "gl_vk_chopper" in directory /home/ra141/vulkan/gl_vk_chopper
linked by target "gl_vk_chopper" in directory /home/ra141/vulkan/gl_vk_chopper
linked by target "shared_sources" in directory /home/ra141/vulkan/shared_sources
-- Configuring incomplete, errors occurred!
See also "/home/ra141/vulkan/gl_vk_chopper/build/CMakeFiles/CMakeOutput.log".
ra141#ra141:~/vulkan/gl_vk_chopper/build$
some help please?
Before running cmake, run the following command export VK_SDK_PATH=/path/to/vulkanSDK/version
Note, that is shall be full path (starting with /)
Be careful to run cmake .. from build folder. From the log, seems you start cmake from subfolder of the build folder. Better yet, clean it before reruning cmake:
cd build
rm -rf ./*
cmake ..

compiler seems cannot find boost/shared_ptr.hpp

I am using Mac with OS 10.11.6
I am trying to install OpenGV on my Mac. This is a part of dependencies that will be needed to build the OpenSfM library. So, what I did is:
brew install homebrew/science/ceres-solver
brew install boost-python
brew install eigen
git clone https://github.com/paulinus/opengv.git
cd opengv
mkdir build
cd build
cmake .. -DBUILD_TESTS=OFF -DBUILD_PYTHON=ON
make install
But I got this error:
In file included from /Users/hilman_dayo/opengv/src/relative_pose/modules/main.cpp:47:
/Users/hilman_dayo/opengv/include/opengv/math/Sturm.hpp:43:10: fatal error: 'boost/shared_ptr.hpp' file not found
#include <boost/shared_ptr.hpp>
^
1 error generated.
make[2]: *** [CMakeFiles/opengv.dir/src/relative_pose/modules/main.o] Error 1
make[1]: *** [CMakeFiles/opengv.dir/all] Error 2
make: *** [all] Error 2
How can I solve this? Already checked, and the file is there at /usr/local/include/boost/shared_ptr.hpp.
If it is ever needed, this is the output during cmake:
-- The C compiler identification is AppleClang 7.3.0.7030031
-- The CXX compiler identification is AppleClang 7.3.0.7030031
-- 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
-- Checking for C++11 compiler
-- Checking for C++11 compiler - available
-- Performing Test SUPPORTS_STD_CXX11
-- Performing Test SUPPORTS_STD_CXX11 - Success
-- Performing Test SUPPORTS_STD_CXX01
-- Performing Test SUPPORTS_STD_CXX01 - Success
-- Found Eigen: /usr/local/include/eigen3 (Required is at least version "2.91.0")
CMake Warning at /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindBoost.cmake:743 (message):
Imported targets not available for Boost version 106200
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindBoost.cmake:842 (_Boost_COMPONENT_DEPENDENCIES)
/usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindBoost.cmake:1395 (_Boost_MISSING_DEPENDENCIES)
python/CMakeLists.txt:2 (find_package)
-- Boost version: 1.62.0
-- Found the following Boost libraries:
-- python
-- Found PythonLibs: /usr/lib/libpython2.7.dylib (found version "2.7.10")
-- Found PythonInterp: /Users/hilman_dayo/.virtualenvs/cv/bin/python2.7 (found version "2.7.12")
-- Found NumPy: version "1.11.1" /Users/hilman_dayo/.virtualenvs/cv/lib/python2.7/site-packages/numpy/core/include
-- Configuring done
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake
--help-policy CMP0042" for policy details. Use the cmake_policy command to
set the policy and suppress this warning.
MACOSX_RPATH is not specified for the following targets:
pyopengv
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done
-- Build files have been written to: /Users/hilman_dayo/opengv/build
OK. Comment by #usr1234567 give me a hint to find the answer. After a quick search, all I need to do is just execute this on the command line:
xcode-select --install

Artery( Veins extension ) example is not working

I do not have much experience with Omnet,I would like to know if I correctly running the command to run the example.
First when I type make Vanetza, I realized that COHDA_MK2_ROOT is missing, however, the Vanetza compiles without error:
mkdir extern/vanetza/build
cd extern/vanetza/build && cmake -DCMAKE_BUILD_TYPE=Release ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.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
-- 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
-- Boost version: 1.58.0
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- date_time
-- serialization
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- date_time
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- date_time
-- Found GeographicLib: /usr/local/include (found suitable version "1.46", minimum required is "1.37")
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- serialization
-- Found CryptoPP: /usr/include (found suitable version "5.6.1", minimum required is "5.6.1")
**-- Could NOT find Cohda (missing: COHDA_MK2_ROOT)**
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- system
-- Configuring done
-- Generating done
When I try to run the ./run command with root into the /artery/scenarios/artery, it returns:
./run: 2: ./run: ../../run: not found
When I try to run the opp_run command into the same folder, I can open the Omnet, however, the simulation does not run since the Scenario.ned is missing.
What can I do?
Those run scripts are obsolete by now. You can run simulations through the new run targets, i.e. the scenario from the scenarios/artery folder can be executed by the run_example target in your build directory.
If you have followed the build instructions in Artery's README then it looks like this:
cd build
make run_example
You can also start the simulation environment trough the debug_exampletarget if you have built with CMAKE_BUILD_TYPE set to "Debug",
PS: "Could NOT find Cohda (missing: COHDA_MK2_ROOT)" is not a severe error at all, it just informs about a build configuration without features requiring this optional dependency.

Coin3D library using cmake on Fedora 20 - fatal error: Inventor/Qt/SoQt.h: No such file or directory

I have create a project that uses Cmake and Coin3D libraries: https://github.com/tuxdna/phyloviz
I am getting a fatal error: Inventor/Qt/SoQt.h: No such file or directory. This is what I am doing:
$ sudo yum install cmake SoQt-devel
$ cd path/to/phyloviz
[phyloviz] $ mkdir build
[phyloviz] $ cd build/
[phyloviz/build] $ cmake ../ -DCOIN3D_INCLUDE_DIRS=/usr/include/Coin2/
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /bin/cc
-- Check for working C compiler: /bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /bin/c++
-- Check for working CXX compiler: /bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found PkgConfig: /bin/pkg-config (found version "0.28")
-- Looking for Q_WS_X11
-- Looking for Q_WS_X11 - found
-- Looking for Q_WS_WIN
-- Looking for Q_WS_WIN - not found
-- Looking for Q_WS_QWS
-- Looking for Q_WS_QWS - not found
-- Looking for Q_WS_MAC
-- Looking for Q_WS_MAC - not found
-- Found Qt4: /bin/qmake-qt4 (found version "4.8.5")
-- Found Coin3D: /usr/lib64/libCoin.so
-- Configuring done
-- Generating done
-- Build files have been written to: .../phyloviz/build
Now lets compile the code
[phyloviz/build] $ make
Scanning dependencies of target ev-iv
[ 50%] Building CXX object CMakeFiles/ev-iv.dir/src/ev-iv/ev-iv.cc.o
.../phyloviz/src/ev-iv/ev-iv.cc:1:30: fatal error: Inventor/Qt/SoQt.h: No such file or directory
#include <Inventor/Qt/SoQt.h>
^
compilation terminated.
make[2]: *** [CMakeFiles/ev-iv.dir/src/ev-iv/ev-iv.cc.o] Error 1
make[1]: *** [CMakeFiles/ev-iv.dir/all] Error 2
make: *** [all] Error 2
[phyloviz/build]$
Even though I have specified the include directory using cmake ../ -DCOIN3D_INCLUDE_DIRS=/usr/include/Coin2/, I am getting the fatal error above.
This works fine on Ubuntu 14.04. How can I resolve this?