C++ external libraries issue [duplicate] - c++

This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I have installed an external library named metis which resides at "/usr/local/opt/" in my system and I want to link this library to my existing make project, which seems to be difficult. I have tried a few reference links here on stackoverflow but they don't seem to work.
My CMakeLists.txt looks like this:
project(Multi)
cmake_minimum_required (VERSION 2.6)
find_package(CGAL QUIET COMPONENTS Core )
include( ${CGAL_USE_FILE} )
include_directories (BEFORE "/include")
include_directories (BEFORE "/dt")
find_package(MPI)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})
SET(CMAKE_C_COMPILER mpicc)
SET(CMAKE_CXX_COMPILER mpicxx)
SET(MPIEXEC_MAX_NUMPROCS "4" CACHE STRING "Maximum number of processors available to run MPI applications.")
find_package(METIS)
include_directories("/usr/local/opt/metis/include")
add_subdirectory("/usr/local/opt/metis/include/bin")
add_executable(example example.cpp)
I have tried to add the location of the library by using add_subdirectory and include_directories tags but they don't seem to work.
Any help on this will be appreciated.

find_package() searches for the library in a set of predefined directores, and /usr/local/opt is very likely not among those. In this case, since you already know where your library is located, you can tell find_package() where to look for it:
find_package(METIS REQUIRED PATHS /usr/local/opt/metis)
Also, add_subdirectory() works only if the specified directory contains a CMakeLists.txt. You need this only if you want to build the library metis as part of your build process, and if that directory is the root of the metis library, and it used CMake for building too. If you only have the header files and pre-build library files, then you do not need this.

Related

Using CMake to create an executable that can run independently on other machines

I’m using LibTorch and OpenCV for a program in Cpp. The compilation and building is done on Linux using CMake. The program builds and runs as expected.
I want to use the executable that CMake created on another Linux machine.
The problem is that I don’t want to install either LibTorch nor OpenCV on the other machine. I’d rather supply the user with a single executable if possible.
How can CMake create a single independent executable?
If making just a single file is irrelevant, how can CMake copy all needed libraries to a single directory?
The current CMake file:
cmake_minimum_required(VERSION 2.8)
project(prediction)
list(APPEND CMAKE_PREFIX_PATH “libtorch”) # the folder where libtorch in found
set(CMAKE_BUILD_TYPE Release)
find_package( OpenCV REQUIRED )
find_package( Torch REQUIRED )
if(NOT Torch_FOUND)
message(FATAL_ERROR “Pytorch Not Found!”)
endif(NOT Torch_FOUND)
message(STATUS “Pytorch status:”)
message(STATUS " libraries: ${TORCH_LIBRARIES}")
message(STATUS “OpenCV library status:”)
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
file(GLOB SOURCES ".h" ".cpp") # Link all headers and sources in root dir
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(entrypoint ${SOURCES})
target_link_libraries(entrypoint ${TORCH_LIBRARIES} ${OpenCV_LIBS})
set_property(TARGET entrypoint PROPERTY CXX_STANDARD 14)
####### EDIT
Thanks for the answers.
Following Phesho_T answer bellow, I got the static compilation of LibTorch, but it won't compile with the set() instruction. It throws C10_LIBRARY NOTFOUND.
I think I'll try to use the shared libraries. How can CMake be instructed to copy the releveant shared libraries to the "build" folder, so I can pack everything in a .zip file and send it to the user.
Like another answer said, you need to link the static libs of Torch and OpenCV in your executable.
There are a few pre-requisites for this:
The two libraries need to have static (.a) libraries installed on your system. If they don't, you may have to manually build them. Steps for this differ between different packages.
You need to tell CMake to search for the static libraries ONLY. This is done via the CMAKE_FIND_LIBRARY_SUFFIXES variable. The chances are the default for this is .so;.a, meaning it will find the shared library first.
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
The fact that you're using variables in your target_link_libraries command, instead of imported libraries (the modern CMake way) makes me think that this should be enough - these variables should expand to full paths to the static libraries, which should then be added to your linker command.
Things are a bit more complicated to explain if imported targets were used, but this might be out-of-scope for this question.
Try it out and let us know how you get on.
To create a single executable you need to statically link the dependencies into your executable. Check your libraries to see if they provide static-libs else you need to recompile libtorch or opencv to make static libraries.

CMakeLists C++ Beginner

I've started playing a little bit with C++ and to make it happen I decided to write a simple game engine.
For this purpose, I'm using CLion as my IDE and it works all good but adding libraries is just a nightmare. First I've installed all required libraries like glew, glfw or glm using brew, all went fine. Then I spent almost 2 hours to get it to work on my project.
My biggest mystery is the reason why it works, I've worked with build systems in java, python or golang and everything was always clear to me. However, I have no idea why it works the way it works and I'd love to know!
Here is my CMakeLists file.
cmake_minimum_required(VERSION 3.10)
project(untitled2)
find_package(GLEW REQUIRED)
find_package(GLFW3 REQUIRED)
set(CMAKE_CXX_STANDARD 17)
add_executable(untitled2 main.cpp)
target_link_libraries(untitled2 ${GLEW_LIBRARIES})
target_link_libraries(untitled2 glfw)
Now I have a few questions:
1. Why am I able to use GLM library without including it in the CMakeLists?
2. Why do I need to include glfw and glew but not glm?
3. Why do I need to use ${GLEW_LIBRARIES} and not some name like glew? (I tried different names, but nothing worked.)
btw. I'm using macOS.
The first thing to remember is that C++ doesn't (yet) have a real module system like newer languages. It just has a list of directories that it searches for header files, a list of directories that it searches for libraries, and a list of libraries to search for symbols when linking. The target_link_libraries directive just adds compiler flags that add to those three lists.
Now, on to this specific scenario. Most of the magic happens in the find_package directive. That directive really just ends up running cmake scripts. Sometimes those are packaged with cmake, sometimes they're installed along with the package you're finding. In the end, those scripts can do basically whatever they want. They all have the same objective, to give you a way to add the appropriate compiler flags to use the package, but there are a couple common ways they do that.
The older way is to set variables that you can use to tell the compiler what directories to search for headers and libraries and what libraries to link to. That's the approach GLEW seems to have taken. It sets the variables GLEW_LIBRARIES and GLEW_INCLUDE_DIRS and you then have to use link_libraries and include_directories to tell the compiler what to do. This was the only approach available in older versions of cmake (pre 2.8 IIRC), so while it's not as nice to use, it is still how many libraries' find_package scripts work.
The newer way is to create imported targets. Those targets have appropriate properties set so that any targets that link to the imported target inherit the appropriate include directories and library flags. This is the approach GLFW took. It creates an imported target named glfw that has the INTERFACE_INCLUDE_DIRECTORIES and INTERFACE_LINK_LIBRARIES properties set. When you pass that to target_link_libraries, your untitled2 target will inherit those include directories and libraries.
Finally, GLM is a header-only library. There are no library files to link to, so as long as the appropriate directory is added to the compilers header search path you'll be able to include and use GLM.
Since you used homebrew to install your libraries, all of their headers are likely under the same base directory; most likely "/usr/local/include". All of their library files are similarly likely under the same directory; probably something "/usr/local/lib". That means that your compiler will be able to find any of their headers and libraries is you tell it to search "/usr/local/include" for headers and "/usr/local/lib" for libraries.
So, to finally answer the question: Thing's work because the glfw target told cmake that it should set the compiler flags to add "/usr/local/include" to its list of include directories. Since that's the same directory it needs to search for GLM and GLEW, the compiler is able to find the headers for all of your libraries. The compiler is also able to find the library files it need to link to because cmake told it to look for them explicitly via the list GLEW_LIBRARIES and the inherited properties from the glfw target. GLM doesn't have any library files to link to, so there's nothing to tell it about.
You really shouldn't rely on everything being in the same place though. You should be able to tell the compiler about everything like this (note that I haven't actually tested this):
cmake_minimum_required(VERSION 3.10)
project(untitled2)
set(CMAKE_CXX_STANDARD 17)
add_executable(untitled2 main.cpp)
# This will fill the variables GLEW_INCLUDE_DIRES and GLEW_LIBRARIES
# that you can use to add the appropriate compiler flags
find_package(GLEW REQUIRED)
# This will create an imported target named glfw that you can link to
# to inherit the appropriate include directories and libraries
find_package(GLFW3 REQUIRED)
# This also creates an imported target named glm that you can "link to"
# to inherit the appropriate include directories
find_package(glm REQUIRED)
# GLEW uses an old-style find_package script, so you have to
# explicitly tell cmake about GLEW's include directories
target_include_directories(untitled2 PUBLIC ${GLEW_INCLUDE_DIRS})
# And the library files to link to
target_link_libraries(untitled ${GLEW_LIBRARIES})
# cmake will automatically add the appropriate include directories
# and library files that the imported glfw target tells it about
target_link_libraries(untitled2 glfw)
# You use the target_link_libraries directive with the glm imported target
# even though you're not actually linking to any libraries. It's just how
# you tell cmake you want your untitled2 target to inherit the appropriate
# include directories from the imported glm target
target_link_libraries(untitled2 glm)

C++ How to run programs in Clion when you need to include OpenGL libraries?

Hello I need to work with OpenGL and want to create my project in Clion. But Clion cannot compile and run my projects because of the libraries I need to include. I can create my own makefile and run the program in terminal, but I want to do it in the IDE. How can I make this happen?
First make sure you installed all libraries correctly using the compiler you configured in clion/cmake. Assuminf you have a fresh CMakeLists.txt like
cmake_minimum_required(VERSION 3.3.2)
project(MyGL CPP)
add_executable(demo-run main.cpp)
For linking your libraries you need two things. First tell the compiler where to find the include files and second which libraries to link. You could just hard code you local installation like
target_link_libraries(demo-run path/to/glfw.lib path/to/opengl.lib path/to/jpeg.lib ...)
target_include_directories(demo-run PRIVATE path/to/glfw/include path/to/opengl/include path/to/jpeg/include ...)
however this is not very portable and if you want to work with another compiler or on another machine your project file will fail. Instead you can use the package system of cmake
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_package(JPEG REQUIRED)
find_package(GLEW REQUIRED)
find_package (OpenGL REQUIRED)
find_package (GLM REQUIRED)
target_link_libraries(demo-run ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${JPEG_LIBRARIES} ${OPENGL_LIBRARIES})
target_include_directories(demo-run PRIVATE ${GLFW_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
The glfw part is a bit tricky and works only on linux i guess see http://www.glfw.org/docs/3.0/build.html.
This code is not tested at all and you may need to specify some enviroment variables so cmake can find the packages or provide additional find scripts like https://github.com/lighttransport/nanogi/blob/master/cmake/FindGLM.cmake.
I would recommend to use the CMake build tool which does the work generating Makefiles for you and is also directly supported by clion. When you open the directory containing a CMakeLists.txt (CMake Project File) with clion, it should be automatically be loaded and compiled (if not just hit build)
A very simple example CMake project would look like this
cmake_minimum_required (VERSION 2.8.9)
project (OpenGl-Stuff)
include_directories(src)
add_executable(your-binary src/your-code.c src/your-code.h)
target_link_libraries(your-binary opengl)
# target_link_libraries will search for libopengl on standard system paths,
# maybe the library is not called libopengl, then you have to adjust the name above
this cmake project will generate the binary for you and link it against opengl

How to specify where to find a shared library using CMake? [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 5 years ago.
I am trying to link to a shared library using CMake. I understand that this can bee achieved using the target_link_libraries() command. I have been able to get this working but not quite in the way that I would like.
Using the below CMakeLists.txt, I've been able to link to the shared library.
cmake_minimum_required(VERSION 3.7)
project(DylibTest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(DylibTest ${SOURCE_FILES})
target_link_libraries(DylibTest
${CMAKE_SOURCE_DIR}/Resources/libDynamicTest.dylib)
This seems to tell the linker to look in the directory that the project is located in. This is fine for development, but how would I distribute this project? End-users will clearly not have this same folder.
I've tried to pass in a relative path like so:
cmake_minimum_required(VERSION 3.7)
project(DylibTest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(DylibTest ${SOURCE_FILES})
target_link_libraries(DylibTest /Resources/libDynamicTest.dylib)
When doing this, I get the following error:
No rule to make target `/Resources/libDynamicTest.dylib', needed by `DylibTest'. Stop.
I've looked at the documentation for target_link_directories() but it has not been helpful. I've also looked at numerous other questions here.
In case it's not clear, my intention is to distribute the executable with a folder named "Resources" that contains the shared library. How should my CMakeLists.txt file look in order to do this?
EDIT: In response to this question being marked as a possible duplicated of this question, I do concede that that question is basically asking the same thing, the answers there do not work for me.
Here's how my CMakeLists.txt looks after trying a accepted answer on the other question:
cmake_minimum_required(VERSION 3.7)
project(DylibTest)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(DylibTest ${SOURCE_FILES})
link_directories(${CMAKE_BINARY_DIR}/Resources)
target_link_libraries(DylibTest DynamicTest)
This unfortunately does not work an results in an error stating that the library could not be found.
Have a look at https://cmake.org/cmake/help/v3.0/command/find_library.html.
You can specify one or more alternative search folder for cmake to look for the library. It also looks in the default library folder for your OS as well.
find_library(YOURLIB
NAMES your // without the lib
PATHS ${CUSTOM_LIB_PATH})
You can then link the library with
target_link_library(targetname ${YOURLIB})
You might be looking for Creating packages with CPack? This lets you describe how your project shall be packaged into a setup routine and what the file layout after installing looks like.

linking opencv libraries included as an external project via cmake [duplicate]

This question already has answers here:
Configuring an c++ OpenCV project with Cmake
(2 answers)
Closed 8 years ago.
I'm relatively new to cmake and after days of struggling couldn't figure out the following thing:
I have a project that depends on opencv (which is a cmake project on its own) and I want to statically link opencv libraries. What I'm doing is I have a copy of opencv source code in my project and include it into my CMakeLists.txt via
ExternalProject_Add(my_copy_of_opencv_project
CMAKE_ARGS -D BUILD_SHARED_LIBS=NO ...
CMAKE_INSTALL_PREFIX=${MY_OPENCV_INSTALL_DIR}
SOURCE_DIR ${PATH_TO_OPENCV_SRCS}
)
All built nicely and where I have problem is that I cannot reliably determine where the opencv libraries will be. E.g. on linux/mac there are in ${MY_OPENCV_INSTALL_DIR}/lib and are named as libopencv_core.a whereas on 32-bit Windows with VS 2012 installed the libs are in ${MY_OPENCV_INSTALL_DIR}/lib/x86/vc11/staticlib and for the Debug configuration the libs named like opencv_core247d.lib.
So the question is can I somehow obtain a list of all libraries produced by the OpenCV build (and the root lib folder) and link them via something like target_link_libraries(mylib opencv_core ...)?
Maybe I'm doing something wrong or overcomplicated. So what I basically want is to compile my embedded opencv source tree statically and link against its libraries in a "cross-plaformish" way.
Any pointers are highly appreciated! Thanks!
The best solution to use cmake with OpenCV project is:
Compile OpenCV as shared / static libraries with OpenCV's cmake build system.
In your project's CMakeLists.txt
For example (CMakeLists.txt):
cmake_minimum_required(VERSION 2.8.12)
project(test_project)
# OpenCV_DIR could also be read from system environment variable.
if(WIN32)
set(OpenCV_DIR "d:/libs/opencv-2.4.8/build")
else()
set(OpenCV_DIR "/usr/lib/opencv")
endif()
find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui)
include_directories(${OpenCV_INCLUDE_DIRS}) # not needed for opencv>=4.0
add_executable(test main.cpp)
target_link_libraries(test ${OpenCV_LIBS})
It works in cross-platforms.