Using CGAL 4.12 header-only in a project - c++

I'm working on a class project in C++ using CLION which requires the CGAL library, and I'm having trouble getting the library to work with my project.
We're trying to get the library working in it's header-only configuration for simplicity, however the documentation for CGAL is a bit thin on details when it comes to this way of doing things. In this part of the installation documentation we're told to extract the CGAL library data somewhere and reference it in a variable in our cmake file. However we can't seem to reference any of CGAL's headers in our project - none of the directories or files can be found at all.
We've tried to fix this ourselves for a while but we don't really know where to look or what the problem is, and the documentation for CGAL isn't being of much help. Below I've attached our CMakeLists file contents, have we done something really wrong or is it as it should be?
cmake_minimum_required(VERSION 3.9)
project(remeshing_project)
set(CMAKE_CXX_STANDARD 17)
if(MINGW OR CYGWIN)
add_definitions(-O3)
endif()
set(ENV{CGAL_DIR} S:/dev/cgal)
add_definitions(-DCGAL_DIR=$(CGAL_DIR))
#add_definitions(-DCGAL_LINKED_WITH_TBB)
#add_definitions(-DCGAL_USE_GMP)
#add_definitions(-DCGAL_USE_MPFR)
#add_definitions(-DCGAL_USE_ZLIB)
link_libraries(../external/glew_2_1_0/lib/Release/Win32/glew32s)
link_libraries(../external/glfw/lib-vc2015/glfw3)
link_libraries(../external/openGL/OPENGL32)
link_libraries(../external/openGL/glut32)
link_libraries(../external/openGL/glu32)
#Header Locations
set(HEADER_DIRS external/tinyply
external/libigl/include/
external/glfw/include/
external/eigen-git-mirror/
external/glew_2_1_0/include/
external/nanoflann/
external/spectra/
external/halfedge/
external/
$(CGAL_DIR)/)
add_executable(Remesher src/main.cpp external/halfedge/trimesh.cpp src/meshMetrics.cpp src/meshMetrics.h src/remeshingTools.cpp src/remeshingTools.h src/dataStructures.h)
target_include_directories(Remesher PRIVATE ${HEADER_DIRS})
Any help would be hugely appreciated, we've been banging our head against this for a while and are at a loss. Thanks in advance!

My guess is that you are simply missing
find_package( CGAL)
if ( CGAL_FOUND )
include( ${CGAL_USE_FILE} )
After that you should check that the CMAKE variable CGAL_DIR points to your extracted directory of CGAL and you should be fine.

You need:
find_package(CGAL QUIET COMPONENTS Core )
if(CGAL_FOUND)
#Don't let CGAL override flags
set(CGAL_DONT_OVERRIDE_CMAKE_FLAGS TRUE CACHE BOOL "Force CGAL to maintain CMAKE flags")
include(${CGAL_USE_FILE})
include(CGAL_CreateSingleSourceCGALProgram)
endif()
And:
add_executable(Remesher src/main.cpp external/halfedge/trimesh.cpp src/meshMetrics.cpp src/meshMetrics.h src/remeshingTools.cpp src/remeshingTools.h src/dataStructures.h)
target_link_libraries(Remesher PRIVATE ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES}
CGAL also typically requires things like Eigen and MPFR. A full working example is here:
https://github.com/acgetchell/CDT-plusplus/releases/tag/0.1.8
I didn't link directly to my CMakeLists.txt because I'm in the process of converting to using the Conan package manager, so it will be quite different soon.

Related

CMake BUILD undefined reference (findpng)

I'm still very new to CMake so feedback is definitely welcome. So, I'm trying to build a simple application that should eventually create a pdf using the library libharu.
I think i figured it out how to link the library. But I still receive build errors for the findpng module (I suppose libharu depends on it)
CMakeLists.txt:
cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR) # current latest stable version (if lower give FATAL_ERROR)
project(pdf_generator VERSION 0.1.0) # name of the project, version.
file(GLOB TARGET_SRC "./src/*.cpp") # Creates variable, using globbing.
include_directories(${PROJECT_SOURCE_DIR}/include) # list of directories to be used as header search paths.
add_executable(main ${TARGET_SRC}) # Create an executable of set of source files [exe name files to bundle].
find_library(libhpdf_location NAMES libhpdf.a) # find the location of libhpdf.a and save the value in the variable libhpdf_location.
message(STATUS ${libhpdf_location}) # print status of variable.
add_library(libhpdf STATIC IMPORTED) # Add library via a static import.
set_target_properties(
libhpdf PROPERTIES
IMPORTED_LOCATION ${libhpdf_location}
)
target_link_libraries(main libhpdf)
I've never worked with that particular library before, but skimming their CMakeLists.txt on GitHub it seems like libharu has optional dependencies on libpdf and zlib. Without knowing how you built your version of libharu I'm going to assume that both are needed.
Luckily, CMake comes with find-modules for both libpng and zlib, so adding the following should work:
find_package(PNG REQUIRED)
find_package(ZLIB REQUIRED)
set_target_properties(libhpdf
PROPERTIES
INTERFACE_LINK_LIBRARIES "ZLIB::ZLIB;PNG::PNG"
)
Looks like all you need to do is tell cmake to link libpng.

cmake cannot find library when linking library to another application

I am trying to make generate a shared library of an existing application, so that i can link it to gtest application.
My main applications cmake file looks like this:
project(audiodLib CXX C)
cmake_minimum_required(VERSION 2.8.7)
## Lets store all the source code in ${SOURCES}
file(GLOB SOURCES src/*.cpp src/controls/*.cpp src/controls/pulse/*.cpp src/modules/*.cpp src/product/*.cpp src/umi/*.cpp src/umi/modules/*.cpp src/umi/soundSettings/*.cpp src/utils/*.cpp utils/*.cpp pmtrace/*.c)
##Lets generate the library
##Please note, here instead of ${SOURCES} if i try to directly add source code, I always get
##an error saying that cmake could not find any *.cpp files
add_library(audiodLib SHARED ${SOURCES})
##Lets link it with libraries
target_link_libraries(audiodLib ${GLIB2_LDFLAGS}
${LUNASERVICE_LDFLAGS}
${PBNJSON_C_LDFLAGS}
${LUNAPREFS_LDFLAGS}
${POWERD_LDFLAGS}
${PMLOGLIB_LDFLAGS}
${NYXLIB_LDFLAGS}
${LIBPBNJSON_LDFLAGS}
${PULSE_LDFLAGS}
${LTTNG_UST_LDFLAGS}
${URCU_BP_LDFLAGS}
${PULSE_SIMPLE_LDFLAGS}
${WEBOSI18N_LDFLAGS}
pthread
rt
dl
-lsnapshot-boot
)
##Lets make this library availabel for other modules
install(TARGETS audiodLib LIBRARY DESTINATION ${WEBOS_INSTALL_LIBDIR})
After compilation, libaudiodLib.so is generated in /usr/lib directory.
And now if I try to access the audiodLib in my gtest code like this:
##${WEBOS_INSTALL_LIBDIR} = /usr/lib/
include_directories(${WEBOS_INSTALL_LIBDIR})
target_link_libraries(${GTEST_EXECUTABLE}
${WEBOS_GTEST_LIBRARIES}
${GLIB2_LDFLAGS}
${LUNASERVICE_LDFLAGS}
${PBNJSON_C_LDFLAGS}
${LUNAPREFS_LDFLAGS}
${POWERD_LDFLAGS}
${PMLOGLIB_LDFLAGS}
${NYXLIB_LDFLAGS}
${LIBPBNJSON_LDFLAGS}
${PULSE_LDFLAGS}
${LTTNG_UST_LDFLAGS}
${URCU_BP_LDFLAGS}
${PULSE_SIMPLE_LDFLAGS}
${WEBOSI18N_LDFLAGS}
pthread
rt
dl
-lsnapshot-boot
-laudiodLib
)
I get the following error:
cannot find -laudiodLib
The folder structure is as follows:
audiod/
CMakeList
|src
|tests
CMakelist
If someone can point out what I am doing wrong, it would be of great help. I am kind of stuck and clueless after spending 2-3 days on this.
Thank you so very much for offering me suggestions, and pointers, I was finally able to sove the problem.
Regarding library not found issue, I resolved it by rearranging the TARGET_LINK_LIBRARIES as follows:
target_link_libraries(audiod
audiodLib
${GLIB2_LDFLAGS}
${LUNASERVICE_LDFLAGS}
${PBNJSON_C_LDFLAGS}
${LUNAPREFS_LDFLAGS}
${POWERD_LDFLAGS}
${PMLOGLIB_LDFLAGS}
${NYXLIB_LDFLAGS}
${LIBPBNJSON_LDFLAGS}
${PULSE_LDFLAGS}
${LTTNG_UST_LDFLAGS}
${URCU_BP_LDFLAGS}
${PULSE_SIMPLE_LDFLAGS}
${WEBOSI18N_LDFLAGS}
pthread
rt
dl
-lsnapshot-boot
)
And how i resolved the BAD RPATH Error during do_package_qa, I have already answerd it here:
bitbake do_package_qa issue contains bad RPATH

Using new tensorflow op in a c++ library that already uses tensorflow as third party

That's my first time asking a question in stackoverflow. I will try my best to formulate my question properly.
I want to use a custom tensorflow op in in a c++ library, which already uses tensorflow as third party. But I just don't know how to use my custom operation in c++ code at all. I'm trying to learn that with the easy ZeroOut example from Tensroflow c++ tutorial. I registered the ZeroOut op for cpu as in https://github.com/MatteoRagni/tf.ZeroOut.gpu: compiling with make worked and I got a .so file in the usr-ops folder, where also the cc file was. Then I tried to add the ZeroOut.so file to my lib as shared library, but it didn't compile. However, before I added my custom op, registered with bazel as described in tensorflow new op tutorial, in the same way and my library compiled. Maybe because the .so file was created in ../bazel-bin/tensorflow/core/user_ops/ . But in this case I'm not able to use the operation as I should. And including ZeroOut.cpp or the .cpp files of my op in my c++ files didn't make any difference until now.
Here is my CMakeList.txt, which also creates the whole library I'm working with:
cmake_minimum_required(VERSION 2.8)
project(Project1)
set(CMAKE_BUILD_TYPE "Release") # Debug Release
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -std=c++14 -O3 -Wall -fopenmp")
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
#-------------path of 3rd party libraries-------------
# special libs.
find_package(Boost COMPONENTS filesystem iostreams regex)
find_package(FFTW)
find_package(NLopt)
find_package(HDF5 COMPONENTS CXX)
set(EXTERN_LIB_ROOT ${PROJECT_SOURCE_DIR}/3rd-party)
set(TENSORFLOW_ROOT /.../tensorflow)
set(TF_INCLUDE_DIRS "${TENSORFLOW_ROOT}" "${TENSORFLOW_ROOT}/bazel- genfiles" "${TENSORFLOW_ROOT}/bazel-tensorflow/external/protobuf_archive/src")
# lib dirs.
set(LUA_LIBRARIES "${EXTERN_LIB_ROOT}/lua/liblua53.so") #5.3.4
set(LINENOISE_LIBRARIES "${EXTERN_LIB_ROOT}/linenoise-ng/build/liblinenoise.so")
set(YACAS_LIBRARIES "${EXTERN_LIB_ROOT}/yacas/cyacas/libyacas/build/libyacas.so")
set(TF_LIBRARIES ${TENSORFLOW_ROOT}/bazel-bin/tensorflow/libtensorflow_cc.so
${TENSORFLOW_ROOT}/tensorflow/core/user_ops/tf.ZeroOut.gpu-master/zero_out.so
${TENSORFLOW_ROOT}/bazel-bin/tensorflow/core/user_ops/MyNewOp.so)
#-------------ssl headers-------------
include_directories(${PROJECT_SOURCE_DIR}/src
${EXTERN_LIB_ROOT}/eigen
${EXTERN_LIB_ROOT}/gnuplot-iostream
${EXTERN_LIB_ROOT}/
${EXTERN_LIB_ROOT}/linenoise-ng/include
${EXTERN_LIB_ROOT}/yacas/cyacas/libyacas/include
${EXTERN_LIB_ROOT}/lua/src
${NLOPT_INCLUDE_DIRS}
${FFTW_INCLUDES}
${TF_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${HDF5_INCLUDE_DIRS}
${TENSORFLOW_ROOT})
option(BUILD_SHARED_LIBS "build shared library" ON)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
#-------------ssl kernel lib-------------
file(GLOB_RECURSE _src_list
LIST_DIRECTORIES false
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/src/*.h" "${PROJECT_SOURCE_DIR}/src/*.cpp" "")
add_library(ssl SHARED ${_src_list})
set(SSL_LIBRARIES ${TF_LIBRARIES} ${LUA_LIBRARIES} ${Boost_LIBRARIES} ${NLOPT_LIBRARIES} ${FFTW_LIBRARIES} ${LINENOISE_LIBRARIES} ${YACAS_LIBRARIES} ${HDF5_CXX_LIBRARIES}) #${TF_LIBRARIES}
target_link_libraries(ssl ${SSL_LIBRARIES} dl)
add_executable(Project1 main.cpp)
target_link_libraries(Project1 ssl)
There is a related question, but there is no clear answer. How to run custom GPU tensorflow::op from C++ code?
What am I doing wrong? I have no experience in shared library, but maybe there are other better ways to do what I'm trying to do...?
Can someone help me, Maybe giving an example of the required CMakeList.txt?
About the errors I got at runtime:
it was a very trivial one, just the function in my .so file couldn't be found if called with the right amount of parameters, if called with the wrong one it just didn't run.
In the meanwhile I found a better workaround. Since I can use my custom op in python, I will embed python in my c++ library, call a python function to make the graph,which will call my custom op to build the graph. But I didn't finish yet.
Using pkg_config it becomes very simple:
cmake_minimum_required(VERSION 3.10)
project(tf-inference)
find_package(PkgConfig)
pkg_check_modules(TensorFlow REQUIRED tensorflow)
link_directories(${TensorFlow_LIBRARY_DIRS})
include_directories(${TensorFlow_INCLUDE_DIRS})
add_compile_definitions(${TensorFlow_CFLAGS_OTHER})
add_executable(tf-inference inference.cpp)
target_link_libraries(tf-inference ${TensorFlow_LIBRARIES})
However that requires your TensorFlow install has the tensorflow.pc file included. On MacOS: brew install libtensorflow will include this file, and the above works automatically.

Using SDL2 with CMake in CLion

After hours of scouring the web and SO for a solution I'm at a standstill. Nothing has worked so far for me...
I'm on Windows, using CLion IDE which uses CMake. My goal is to correctly link SDL2 to my project and use it through #include "SDL.h" which is the correct way.
The format of my CMakeLists.txt file
Specifics regarding the directory where I should have put the MingW development library of SDL2
Any requirements regarding windows ENV variables that I might have to set.
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.6)
project(sdl2Project)
set(CMAKE_CXX_STANDARD 11)
#This is where sdl2-config.cmake is located
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:/Users/MyUserName/CLibraries/SDL2-2.0.5/x86_64-w64-mingw32/lib/cmake/SDL2")
set(SOURCE_FILES main.cpp)
add_executable(sdl2Project ${SOURCE_FILES})
find_package(sdl2 REQUIRED)
target_include_directories(sdl2Project PUBLIC ${SDL2_INCLUDE_DIRS})
target_link_libraries(sdl2Project ${SDL2_LIBRARIES})
There is no FindSDL2.cmake file used.
The SDL2 library I downloaded from libsdl.org is located in:
C:/Users/MyUserName/CLibraries/SDL2-2.0.5/x86_64-w64-mingw32
I have no experience with CMake so I'm unable to truly understand where the problem stems from. What are the steps I need to take in order for it to find the library and link it correctly??
EDIT:
My Project structure is the following:
sdl2Project
cmake-build-debug
CMakeLists.txt
main.cpp
Looking in your FindSDL2.cmake, you need to provide an hint to CMake about where the library is installed. You could do this by setting an environment variable SDLDIR, but you shouldn't. General advice: you shouldn't use a CMake package that wasn't provided with the sources you're using.
Looking in sources of SDL2, root directory contains a file sdl2-config.cmake.in that should have been configured and installed in your install directory as sdl2-config.cmake: that's the package file you should use.
Am I right guessing the file C:/Users/MyUserName/CLibraries/SDL2-2.0.5/sdl2-config.cmake exists?
If yes, to allow CMake to find it, add your install directory to CMAKE_PREFIX_PATH, before calling find_package:
set(CMAKE_PREFIX_PATH
${CMAKE_PREFIX_PATH}
"C:/Users/MyUserName/CLibraries/SDL2-2.0.5"
)
find_package(sdl2 REQUIRED)
Note the use of "/" in the path instead of "\" which could be interpreted as escaping character. Quotes around the path are only necessary if the path contains whitespaces.
EDIT:
Moreover, you misused target_link_libraries with a wrong target: SDL2 which you don't build in your project, instead of sdl2Project.
You also used a wrong variable: SDL2_LIBRARY instead of SDL2_LIBRARIES; you can see the good variable name by looking in sdl2-config.cmake.
You may consider target_include_directories instead of include_directories, but again the variable name you used is wrong: SDL2_INCLUDE_DIR instead of SDL2_INCLUDE_DIRS.
Try:
target_include_directories(sdl2Project PUBLIC ${SDL2_INCLUDE_DIRS})
target_link_libraries(sdl2Project ${SDL2_LIBRARIES})

Cmake cannot find library using "link_directories"

I Ubuntu, I am learning about cmake and make, and just trying a simple example. I have two directories: src and build. In src, I have two files: main.cpp, and CMakeLists.txt, which has (only) the following text:
add_executable(test main.cpp)
link_directories(/usr/lib/x86_64-linux-gnu)
target_link_libraries(test protobuf)
In /usr/lib/x86_64-linux-gnu, there is a shared library called libprotobuf.so, which I want to link against. My main.cpp uses functions in this library, by including the releveant header file, #include <google/protobuf/message.h>.
Now, in my build directory, I run cmake ../src, and then make. However, I then get linker errors telling me that there are undefined references to some of the functions in the protobuf library. If I do a search through all the files and subdirectories in build, there is not mention of anything related to protobuf.
However, if I remove the link_directories line in my CMakeLists.txt file, and instead write the full path to the library when specifying the executable, i.e. target_link_libraries(test /usr/lib/x86_64-linux-gnu/libprotobuf.so), it compiles and links fine.
Why is link_directories not allowing cmake to find this library?
Do not use link_directories like this in CMake.
This is a common beginner's mistake, as many other build environments work like this, but in CMake it's just asking for trouble. Even the official documentation specifically advises against it:
Note that this command [link_directories] is rarely necessary. Library locations returned
by find_package() and find_library() are absolute paths. Pass these
absolute library file paths directly to the target_link_libraries()
command. CMake will ensure the linker finds them.
So instead, always pass absolute paths to target_link_libraries and use find_library to resolve the link directory:
find_library(PROTOBUF_LIBRARY protobuf HINTS /usr/lib/x86_64-linux-gnu)
target_link_libraries(test PUBLIC ${PROTOBUF_LIBRARY})
This has the huge benefit that you will probably get a diagnostic at CMake configure time if the expected library cannot be found, instead of a random linker error at compile time. Also, this allows the user to specify a library location via the GUI if the target machine has a non-standard directory layout.
So if it doesn't work right away, be sure to check the result of the find_library call and consult the official documentation to track down why it doesn't find your library as intended.
Make sure that your call to link_directories takes place before your call to the relevant add_executable.
I had mistakenly believed it only needed to be before the call to target_link_libraries, but that's not the case. After moving the call, the library is linked properly.
Make sure that the order will be link_directories, set PROJECT_LINK_LIBS, add_executable and then target_link_libraries.
Below is example to demonstarte it:
cmake_minimum_required(VERSION 2.8.9)
project (Logging)
include_directories(include)
file(GLOB LOGGINGSOURCES "libsrc/*.cpp")
file(GLOB SOURCES "src/*.cpp")
add_library(convertString SHARED ${LOGGINGSOURCES})
install(TARGETS convertString DESTINATION /root/Deepak/)
link_directories( /root/Deepak/ )
set(PROJECT_LINK_LIBS libconvertString.so)
add_executable(hello ${SOURCES})
target_link_libraries(hello ${PROJECT_LINK_LIBS} )
Perhaps it's very old topic but none of proposed solutions worked for me. So I had to make my own dirty hack. I do crosscompiling with buildroot and include toolchainfile.cmake.
#...
set(LIB_PATH ${PROJECT_SOURCE_DIR}/relative/path/to/your/lib)
#...
include_directories(/path/to/library/include)
set(LIB_MYLIB ${LIB_PATH}/libmylib.so)
#...
add_executable(${PROJECT_NAME} ${APP_SOURCES})
target_link_libraries(${PROJECT_NAME}
${LIB_MYLIB}
)
Hope this will help