I am trying to use the CGAL library to display a 2D Delaunay triangulation, like in the example here
My code looks like this:
#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/draw_triangulation_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Triangulation;
typedef Triangulation::Point Point;
int main(void){
Point a(1,1), b(2,1), c(2,2), d(1,2);
Triangulation T;
T.insert(a);
T.insert(b);
T.insert(c);
T.insert(d);
CGAL::draw(T);
return 0;
}
When I try to compile this code with g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp the program compiles successfully, but on runtime I get Impossible to draw because CGAL_USE_BASIC_VIEWER is not defined
By searching on Google, I found someone that suggested using g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp -DCGAL_USE_BASIC_VIEWER which produces the following error on compile time: /usr/include/CGAL/Qt/Basic_viewer_qt.h:30:10: fatal error: QApplication: No such file or directory #include <QApplication>
I am using ubuntu 19.04, so I installed CGAl using sudo apt-get install libcgal-dev and sudo apt-get install libcgal-qt5-dev
I tried to install sudo apt-get install libqt5svg5-dev libqt5opengl5-dev as well to solve the error, but to no avail.
Do I need to install additional libraries? Maybe the compilation must be done differently?
Thank you
Ok, for anyone facing the same problem, here is how I solved it:
First, I used the locate QApplication command to find the location of the QApplication header file on my system. Be sure to run sudo updatedb before using locate. If locate doesn't find the location of QApplication then you are missing qt libraries. Try sudo apt-get install qt5-default and the other libraries I mentioned in my question, run sudo updatedb and try locate QApplication again.
When you find the path to QApplication just use the -I option to instruct the compiler to use it. Here is an example g++ -o delaunayTest delaunayTest.cpp -lCGAL -lgmp -lCGAL_Qt5 -DCGAL_USE_BASIC_VIEWER -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets/ (because in my case, QApplication was inside the directory /usr/include/x86_64-linux-gnu/qt5/QtWidgets/)
Trying to compile with this, you will probably get another header file error. Repeat the same process using locate until you get no more header file errors.
At that point, you will likely encounter an undefined reference to symbol error.
To solve this, use locate again to find the location of the file that caused the error (for example libQt5OpenGL.so.5) and add the path to the compilation command as is (for example g++ -o delaunayTest delaunayTest.cpp /usr/lib/x86_64-linux-gnu/libQt5OpenGL.so.5) along with all the previous options.
You will probably get several undefined reference to symbol errors as well. Just keep using the same method until you don't get any.
At this point the program should compile properly and run properly.
Note that if you have multiple versions of qt installed, then the above might not work properly (If for example you have software that uses qt like MATLAB or anaconda installed in your system. You will know because locate will produce many paths for each file on the steps above). In such a case, I suggest building a Virtual Machine, downloading the CGAL libraries and qt5-default and following the above steps there, since it is very likely this won't work in a system with multiple qt installations.
Another option (maybe the easiest), using CMake, is to generate the file using the builtin script:
From the source file directory, run cgal_create_CMakeLists -c Qt5
Edit the generated CMakeLists.txt adding the line add_definitions(-DCGAL_USE_BASIC_VIEWER)
My generated and edited CMakeLists.txt:
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
cmake_minimum_required(VERSION 3.1...3.15)
project( tmp_cgal )
# CGAL and its components
find_package( CGAL QUIET COMPONENTS Qt5 )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
add_definitions(-DCGAL_USE_BASIC_VIEWER) # <==== I've added this
# 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()
# include for local directory
# include for local package
# Creating entries for all C++ files with "main" routine
# ##########################################################
create_single_source_cgal_program( "b.cpp" )
Create a build directory: mkdir build
Change directory: cd build
Generate the build files: cmake ..
Build: make
Run the binary file. For me it's ./b because my source file was b.cpp
Environment:
These instructions should work for an Ubuntu 20.04.1 (or similar) with the packages libcgal-dev, libcgal-qt5-dev and qtbase5-dev installed (and, of course, cmake, make and g++).
Main references:
doc1 and doc2
Related
I am trying to use the Build --> Compile functionality of Visual Studio in an almost "Hello World!" program.
On a new CMake Project, I set up CMakeSettings configuration to WSL-Clang-Debug.
Then I edited CMakeProject3.cpp to include some header file (gdal.h), which is found by CMake when it generates its cache.
// CMakeProject3.cpp : Defines the entry point for the application.
//
#include "CMakeProject3.h"
#include "gdal.h"
using namespace std;
int main()
{
cout << "Hello CMake." << endl;
return 0;
}
The CMakeLists.txt file looks like this:
# CMakeList.txt : CMake project for CMakeProject3, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
add_executable (CMakeProject3 "CMakeProject3.cpp" "CMakeProject3.h")
# TODO: Add tests and install targets if needed.
find_package(GDAL)
target_link_libraries(CMakeProject3 PRIVATE GDAL::GDAL)
Building, which I thought meant compiling and linking succeeds, but if I try to compile only, it fails:
cd /mnt/c/Users/user/source/repos/CMakeProject3/out/build/WSL-Clang-Debug;export CFLAGS=-fno-limit-debug-info;export CXXFLAGS=-fno-limit-debug-info;clang++ -fno-limit-debug-info -g -I"C:\Users\user\AppData\Local\Microsoft\Linux\HeaderCache\1.0\wsl_wsl\usr\include\gdal" -c "/mnt/c/Users/user/source/repos/CMakeProject3/CMakeProject3/CMakeProject3.cpp"
Build failed.
If I try to run the exact same command in WSL, it's even more obvious why it fails. It can't find gdal.h. This is not because it doesn't exist under C:\Users\user\AppData\Local\Microsoft\Linux\HeaderCache\1.0\wsl_wsl\usr\include\gdal, but because in wsl it's supposed to be under /mnt/c/Users/andrei/AppData/Local/Microsoft/Linux/HeaderCache/1.0/wsl_wsl/usr/include/gdal
If I run the above command with the correct path style, the compiling succeeds.
How do I convince Visual Studio to not try to use windows style paths in wsl?
If you want to try the exact same example, you'll have to first run these in wsl:
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt update
sudo apt install libgdal-dev
sudo apt install openssh-server g++ gdb make ninja-build rsync zip
Is this a bug in Visual Studio, or am I not setting something somewhere that I should be setting?
I want to use boost graph library in one of the LLVM passes I am working on. Boost graph library is header only. So usually I can use it as -I<path to boost source>. My question is how to use it inside LLVM source tree (inside lib/Transforms/MyPass).
I tried the following.
First I tired adding include directory to my pass's CMakeLists.txt like so,
target_include_directories(LLVMMyPass "<path to boost headers>")
Next I tried to change the CPP_FLAGS,
set(CPP_FLAGS "${CPP_FLAGS} -I<path to boost headers> ")
Both didn't work. I keep getting the error.
fatal error: boost/graph/graph_traits.hpp: No such file or directory
#include "boost/graph/graph_traits.hpp"
Update : I tried using -DCMAKE_CXX_FLAGS=-I<path to boost> in cmake command. That also did not help.
system-wide installation of boost solved the problem
sudo apt-get install libboost-all-dev
I am trying to use opencv in a project, and am running into problems 'installing' it. I have extracted the opencv files and have created a small test program:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main(int argc, char **argv){
cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",1);
if (im.empty()){
std::cout << "Cannot open image." << std::endl;
} else {
cv::imshow("image",im);
cv::waitKey(0);
}
return 0;
}
To compile the program I have used the command below:
g++ -I"../../PortableGit/opt/opencv/build/include/" -L"../../PortableGit/opt/opencv/build/x64/vc15/lib" main.cpp -lopencv_core -lopencv_highgui -o main
I get the errors below:
In file included from ../../PortableGit/opt/opencv/build/include/opencv2/core.hpp:3293:0,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui.hpp:46,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui/highgui.hpp:48,
from main.cpp:1:
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:714:14: error: 'recursive_mutex' in namespace 'std' does not name
a type
typedef std::recursive_mutex Mutex;
^~~~~~~~~~~~~~~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
typedef std::lock_guard<cv::Mutex> AutoLock;
^~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:34: error: template argument 1 is invalid
typedef std::lock_guard<cv::Mutex> AutoLock;
I believe that it has something to do with mingw binaries no longer being included with opencv. I am missing the opencv/build/x86/mingw directory.
My questions are:
How do I 'install' opencv and use it without also installing some sort of IDE and/or CMake? (I prefer to use vim and the command line.)
Once installed, what command do I use to compile and link a program with opencv?
Any help is appreciated.
Edit:
This appears to be a problem with GCC's implementation of threads on windows. Using mingw-w64 instead of mingw fixed the std::recursive_mutex issue, but now the linker cannot find the proper files.
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_core
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui
After quite a bit of trying things out, this is what I got to work. Oddly, following the LINUX guide to install opencv worked better than the WINDOWS guide, even though I have a windows computer.
Guide to Installing OpenCV on Windows Without VS
Heads-up: This is a multi-step process, 3 separate tools are required. Be prepared for this to take a while.
Part 1: Get everything ready
Download MinGW-w64.
On the downloads page, click on the "MinGW-w64-builds" option. Do not click on the "win-builds" option.
The reason MinGW-w64 has to be used is because it is a newer version of the MinGW compiler suit that has been improved for windows. This means that it supports the posix thread system, where as the standard MinGW compiler only supports the win32 thread system. OpenCV relies on the posix thread system, necessitating the MinGW-w64 compiler.
Extract the MinGW-w64 zip folder to a directory. In my case its PortableGit/opt/MinGW-w64
At this point, you can add the MingGW-w64/mingw32/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the g++ executable directory to run it. This is up to your discretion.
Download an opencv release.
Do not download the package for windows, click the button that says "sources"
Extract the opencv sources zip folder to a directory. In my case its PortableGit/opt/opencv-4.3.0
Also download the opencv_contrib source files directly from the repository.
Extract that folder and place it inside the top level opencv folder: PortableGit/opt/opencv-4.3.0/opencv_contrib in my case.
Download CMake.
I downloaded the zip folder, but you can download the installer if you wish.
Extract the CMake zip folder if you downloaded that, or run the installer. I put my CMake folder here: PortableGit/opt/cmake-3.17.1-win32-x86
At this point, you can add the cmake-3.17.1-win32-x86/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the cmake executable directory to run it. This is up to your discretion.
Part 2: Build OpenCV
Navigate to the opencv directory and create a build folder and cd into it.
mkdir build && cd build
Run the following export commands.
export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
This is to make sure the next cmake command uses the proper compilers.
Run the following cmake command from within that folder:
PortableGit/opt/cmake-3.17.1-win32-x86/cmake.exe -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DOPENCV_VS_VERSIONINFO_SKIP=1 -DOPENCV_EXTRA_MODULES_PATH="/PortableGit/opt/opencv-4.3.0/opencv_contrib/modules/" ..
The -G flag specifies that we are creating build files for the MinGW compiler
The -DCMAKE_BUILD_TYPE=Release specifies that we are building the release version of opencv and not the debug version.
The DOPENCV_EXTRA_MODULES_PATH needs to be set to the modules folder inside the opencv_contrib folder. For me it was PortableGit/opt/opencv-4.3.0/opencv_contrib/modules
The DOPENCV_VS_VERSIONINFO_SKIP specifies to not include version info. If not set, the compiler will throw an error complaining about not having version files. (Shown below for reference.)
gcc: error: long: No such file or directory
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1341:
modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
If successful, the cmake command will finish like this:
Now run this command, again from the build folder: /PortableGit/opt/MinGW-w64/mingw32/bin/mingw32-make.exe -j7
mingw32-make.exe is the windows equivalent of the Linux make command.
The -j7 option run the process with a maximum of 7 threads.
This will take a while! It took my laptop ~20 minutes to complete
If the make command ends in an error, make sure to reset your build directory before continuing any troubleshooting. This is done through this series of commands
rm -rf build
mkdir build
cd build
Part 3: Using OpenCV
To use the opencv library that you just compiled in a project of your own, compile the project with these flags from your projects main directory.
Remember that your compiler now has to be set to the mingw-w64 compiler for opencv support.
I added indentation and newlines for readability, but when entering this in the terminal do not include the newlines or indents.
The number at the end of the linker options may change depending on the version of opencv you downloaded. I downloaded opencv-4.3.0, making my number 430, but yours may be different.
PortableGit/opt/MinGW-w64/bin/g++.exe
-I../../PortableGit/opt/opencv-4.3.0/include/
-I../../PortableGit/opt/opencv-4.3.0/build/
-I../../PortableGit/opt/opencv-4.3.0/modules/core/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/calib3d/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/dnn/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/features2d/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/flann/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/gapi/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/highgui/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/imgcodecs/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/imgproc/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/ml/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/objdetect/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/photo/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/stitching/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/ts/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/video/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/videoio/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/world/include/
-L../../PortableGit/opt/opencv-4.3.0/build/lib/
*.hpp
*.cpp
-lopencv_calib3d430
-lopencv_core430
-lopencv_dnn430
-lopencv_features2d430
-lopencv_flann430
-lopencv_highgui430
-lopencv_imgcodecs430
-lopencv_imgproc430
-lopencv_ml430
-lopencv_objdetect430
-lopencv_photo430
-lopencv_stitching430
-lopencv_video430
-lopencv_videoio430
-o
main
Or you could download VS. Up to you. Hope this helps.
Correction for JackCamichael's answer
those 2 commands won't work in Windows
export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
This should be
setx -m CC C:\msys64\mingw64\bin\gcc.exe
setx -m CXX C:\msys64\mingw64\bin\g++.exe
C:\msys64\mingw64\bin is mingw64 path on my machine
[Ubuntu]
I have compiled CGAL locally:
/path/to/cgal/
/lib/
libCGAL_Core.so libCGAL_Core.so.13.0.2
libCGAL_ImageIO.so.13 libCGAL.so libCGAL.so.13.0.2
libCGAL_Core.so.13 libCGAL_ImageIO.so
libCGAL_ImageIO.so.13.0.2 libCGAL.so.13
/include/
/CGAL/
version.h compiler_config.h
And I have managed to satisfy all of the graph-tool requirements except cgal (at least all of the requirements checked up to cgal):
./configure --with-boost=/path/to/boost --with-cgal=/path/to/cgal
And I get all successes up and until I get the following error message:
checking for __gmpz_init in -lgmp... yes
checking for __gmpz_init in -lgmp... (cached) yes
checking whether CGAL is available in /path/to/cgal... no
configure: error: CGAL library not found.
// the harshest part is that it seems to be searching in the correct
// directory.
I have tried specifying different points in the cgal build directory. The cgal compilation command I used was (from build directory):
cmake path/to/cgal_src_dir -DCMAKE_BUILD_TYPE=Release;
Next, I tried adding includes:
./configure --with-boost=$boost --with-cgal=path/to/cgal CPPFLAGS="-I path/to/cgal/include -I $HOME/.local/include" LDFLAGS="-L path/to/cgal/lib -L $HOME/.local/lib -Wl,-rpath=$HOME/.local/lib"
I will admit that I don't understand the -Wl,-rpath= part, I copied that from the graph-tool installation guide. The .local/lib folder contains the files for the other components, such as gmp, expat, sparsehash, etc.
This is not exact answer but as asked by OP will help in finishing installation, so please don't vote blindly.
To create debian package of libcgal open your CMakeList.txt and at the end of file add:
#--------------------------------------------------------------------
# Create debian files
#--------------------------------------------------------------------
if (UNIX AND NOT APPLE)
SET(CPACK_GENERATOR "DEB")
SET(CPACK_PACKAGE_NAME "libcgal-all")
SET(CPACK_PACKAGE_VERSION "${CGAL_VERSION}")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "C++ library for computational geometry (development files)\n CGAL (Computational Geometry Algorithms Library) makes the most important of the solutions and methods developed in computational geometry available to users in industry and academia in a C++ library. The goal is to provide easy access to useful, reliable geometric algorithms.\n .\n This package contains the header files and static libraries for libCGAL.so, libCGAL_Core.so, and libCGAL_ImageIO.so. The header files and static libraries for libCGAL_Qt4.so can be found in the package libcgal-qt4-dev.")
SET(CPACK_PACKAGE_CONTACT "bordeo")
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libboost-dev, libboost-thread-dev, libboost-system-dev, libboost-program-options-dev, libgmp10-dev, libmpfr-dev, zlib1g-dev")
SET(CPACK_DEBIAN_PACKAGE_REPLACES "libcgal10, libcgal-dev")
INCLUDE(CPack)
endif()
In case you don't have any dependency remove whole line of SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libcln6, libcln-dev, libreadline6, libreadline6-dev, flex, bison"), and change others as it seems fit.
Now go to the terminal and issue following commands in cgal directory
mkdir build
cd build
cmake-gui ..
# set CMAKE_INSTALL_PREFIX to `~/.local
cmake ..
make -j4
cpack ..
you will find your debian built. Extract or install the debian to ~/.local.
Once this is done go to graph tool directory and start the build like
./configure --prefix="/wherever" --with-boost=/path/to/boost --with-cgal=~/.local
make -j4
make install
Hope this will solve your problem.
I have this problem, I am trying to build caffe on debian machine, I will build everything but at the end at linking I get multiple undefined references to google::protobuf::...
I am attaching file with build log, containing error messages (build_caffe.txt).
Both libprotobuf-dev and protoc are installled. (output of dpkg -s is in proto.txt)
Here is how I build caffe.
export CXX=g++-4.9
export CC=gcc-4.9
cmake -D CUDA_HOST_COMPILER=/usr/bin/x86_64-linux-gnu-gcc-4.9 -D CUDA_USE_STATIC_CUDA_RUNTIME=OFF ..
make all
Does anyone know a solution for this problem please?
proto.txt
build_caffe.txt
Looks like your protobuf was compiled using a different version of gcc. Try to remove protobuf from your system and install it from sources, using the same gcc version you would like to use fro Caffe. (/usr/bin/x86_64-linux-gnu-gcc-4.9 according to your command).
EDIT:
If you can't install the updated protobuf, edit $CAFFE_ROOT/cmake/ProtoBuf.cmake and make the following changes:
#find_package( Protobuf REQUIRED ) # 1. Comment out this line
# 2. explicitly define protobuf's directories
set(PROTOBUF_INCLUDE_DIR path_to_protobuf/src/google/protobuf)
set(PROTOBUF_LIBRARIES path_to_where_protobuf_libs_are_built_to)
# 3. Explicitly set the full path to protoc executable
set(PROTOBUF_PROTOC_EXECUTABLE path_to_where_the_new_protoc_executable_is_build_to)
# ... Continue as usual
list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${PROTOBUF_INCLUDE_DIR})
list(APPEND Caffe_LINKER_LIBS PUBLIC ${PROTOBUF_LIBRARIES})
#...