How to add external library to CMakeLists? - makefile

Normal compilation (works fine):
g++ DBHandler.cpp Functions.cpp Main.cpp -I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq -o dbhandler
It`s possible to run:
./dbhandler
CMakeLists.txt (standard):
cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler config++ pqxx pq)
add_executable(dbhandler ${SOURCES})
How to change CMakeLists.txt and add:
-I/usr/local/include -L/usr/local/lib -lconfig++ -lpqxx -lpq
to compile program using cmake?

The correct solution is to use the CMake package PkgConfig to use pkg_search_module
Your file will become:
cmake_minimum_required(VERSION 2.8.9)
project(DBHandler)
find_package(PkgConfig REQUIRED)
pkg_search_module(CONFIGPP REQUIRED config++)
pkg_search_module(PQ REQUIRED pq)
pkg_search_module(PQXX REQUIRED pqxx)
include_directories(include ${CONFIGPP_INCLUDE_DIRS} ${PQ_INCLUDE_DIRS} ${PQXX_INCLUDE_DIRS})
file(GLOB SOURCES "src/*.cpp")
target_link_libraries(dbhandler ${CONFIGPP_LIBRARIES} ${PQ_LIBRARIES} ${PQXX_LIBRARIES})
add_executable(dbhandler ${SOURCES})

Related

How do I get CMake to find OpenMP_C, OpenMP_CXX, etc.?

I am fairly new to working with the terminal and I am attempting to use cmake to configure a build for a project that involves public transportation routes. When I attempt to make the build I get the following response in the terminal:
$ cmake ..
-- Could NOT find OpenMP_C (missing: OpenMP_C_FLAGS OpenMP_C_LIB_NAMES)
-- Could NOT find OpenMP_CXX (missing: OpenMP_CXX_FLAGS OpenMP_CXX_LIB_NAMES)
-- Could NOT find OpenMP (missing: OpenMP_C_FOUND OpenMP_CXX_FOUND)
CMake Warning at CMakeLists.txt:33 (message):
Configuring without OpenMP!
CMake Error at src/CMakeLists.txt:13 (add_subdirectory):
The source directory
/Users/ericbush/Desktop/the-one/toolkit/gtfs/pfaedle/src/cppgtfs
does not contain a CMakeLists.txt file.
CMake Error at src/CMakeLists.txt:14 (add_subdirectory):
The source directory
/Users/ericbush/Desktop/the-one/toolkit/gtfs/pfaedle/src/configparser
does not contain a CMakeLists.txt file.
-- Configuring incomplete, errors occurred!
See also "/Users/ericbush/Desktop/the-one/toolkit/gtfs/pfaedle/build/CMakeFiles/CMakeOutput.log".
See also "/Users/ericbush/Desktop/the-one/toolkit/gtfs/pfaedle/build/CMakeFiles/CMakeError.log".
Below is what I believe to be the relevant code from the CMakeLists.txt file:
find_package(OpenMP)
if (OPENMP_FOUND)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
# set compiler flags, see http://stackoverflow.com/questions/7724569/debug-vs-release-in-cmake
if(OPENMP_FOUND)
set(CMAKE_CXX_FLAGS "-fopenmp -Ofast -fno-signed-zeros -fno-trapping-math -Wall -Wno-format-extra-args -Wextra -Wformat-nonliteral -Wformat-security -Wformat=2 -Wextra -Wno-implicit-fallthrough -pedantic")
else()
message(WARNING "Configuring without OpenMP!")
set(CMAKE_CXX_FLAGS "-Ofast -fno-signed-zeros -fno-trapping-math -Wall -Wno-format-extra-args -Wextra -Wformat-nonliteral -Wformat-security -Wformat=2 -Wextra -Wno-implicit-fallthrough -pedantic")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "-Og -g -DLOGLEVEL=3 -DPFAEDLE_DBG=1")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -DLOGLEVEL=2")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -DLOGLEVEL=2")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -g -DLOGLEVEL=3")
I have found some similar questions on this and other websites, but I am still struggling to understand the problem because I have never used cmake before and, as mentioned, I am somewhat new to working with the terminal. I would be extremely grateful for any information on what the error messages are actually telling me, or any suggestions on steps I could take/things I could try to solve the issue. I am working on a Mac, if anyone is wondering.
For the compiler to see OpenMP, you may need to set the following options in your cmake command (for dependencies located in /opt/local):
cmake .. \
-DOpenMP_C_FLAGS=-fopenmp=lomp \
-DOpenMP_CXX_FLAGS=-fopenmp=lomp \
-DOpenMP_C_LIB_NAMES="libomp" \
-DOpenMP_CXX_LIB_NAMES="libomp" \
-DOpenMP_libomp_LIBRARY="/opt/local/lib/libomp.dylib" \
-DOpenMP_CXX_FLAGS="-Xpreprocessor -fopenmp /opt/local/lib/libomp.dylib -I/opt/local/include" \
-DOpenMP_CXX_LIB_NAMES="libomp" \
-DOpenMP_C_FLAGS="-Xpreprocessor -fopenmp /opt/local/lib/libomp.dylib -I/opt/local/include"
The two missing CMakeLists.txt are located in git submodules within src/ To download the submodules, cd to the root phaedle directory and issue the following command:
git submodule update --init --recursive
Edit: this is now fixed in master.

Converting simple makefile into CMakeLists.txt?

I'm trying to convert this simple makefile into a CMakeLists.txt. I'm specifically struggling with the flags.
SOURCE = triangle.cpp shader.cpp
CC = g++
CFLAGS = -Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw
OBJECT = window
default:
$(CC) -o $(OBJECT) $(SOURCE) $(CFLAGS)
clean:
rm -f $(OBJECT)
Here's my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Graphics)
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw")
#add_definitions(${CMAKE_CXX_FLAGS})
add_executable(
trtangle
triangle.cpp
)
Here's the error message I get:
Scanning dependencies of target triangle
[ 50%] Building CXX object CMakeFiles/triangle.dir/triangle.cpp.o
clang: warning: -framework OpenGL: 'linker' input unused [-Wunused command-line-argument]
clang: warning: -framework GLUT: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lGLEW: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lglfw: 'linker' input unused [-Wunused-command-line-argument]
/Users/neilculbertson/Desktop/OpenGL/triangle.cpp:2:10: fatal error: 'GL/glew.h' file not found
#include <GL/glew.h>
^~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/triangle.dir/triangle.cpp.o] Error 1
make[1]: *** [CMakeFiles/triangle.dir/all] Error 2
make: *** [all] Error 2
I've also tried setting CMAKE_C_FLAGS But still no luck. Keep in mind when I use the makefile alone, everything compiles and works perfectly. I'm wondering if it's a GL error? or maybe I installed GLEW weirdly?
cmake
cmake_minimum_required(VERSION 3.0) # This line is required
project(triangle) # This line is required
make
CC = g++
CFLAGS = -Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw
cmake
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -framework OpenGL -framework GLUT -lGLEW -lglfw")
make
SOURCE = triangle.cpp shader.cpp
default:
$(CC) -o $(OBJECT) $(SOURCE) $(CFLAGS)
cmake
# The name of the result and all required sources
add_executable(triangle triangle.cpp shader.cpp)
# These libraries are required
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glfw3 3.0 REQUIRED)
# ... and the path to the header files and so files
target_include_directories(triangle ${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
target_link_libraries(triangle ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} glfw3)
Other possibility is to collect all sources within a list at first.
set(SOURCES triangle.cpp shader.cpp)
add_executable(triangle ${SOURCES)
make
OBJECT = window
cmake
Not necessary
make
clean:
rm -f $(OBJECT)
cmake
This is built in.
cmake_minimum_required(VERSION 3.0)
project(Graphics)
add_executable(
trtangle
triangle.cpp
)
target_compile_options(trtangle PRIVATE -Wall -Wextra -pedantic)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glfw3 3.3 REQUIRED)
target_include_directories(trtangle ${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS})
target_link_libraries(trtangle ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES} glfw)

How to force cmake put options after filename?

By some reason the following do now work
/usr/bin/c++ -lm -L/usr/X11R6/lib -lX11 -lpthread CMakeFiles/net.dir/advanced.cpp.o -o net -rdynamic
But this do:
/usr/bin/c++ -lm -L/usr/X11R6/lib CMakeFiles/net.dir/advanced.cpp.o -lX11 -lpthread -o net -rdynamic
My question is how to force CMake to put option AFTER /advanced.cpp.o to make compilation possible in my case. All CMake options I used put my custom GCC option before compiler.
My cmake file
cmake_minimum_required (VERSION 2.6)
project (mnist)
set( CMAKE_VERBOSE_MAKEFILE on )
add_compile_options("-O2")
add_compile_options("-std=c++11")
SET( CMAKE_EXE_LINKER_FLAGS "-L/usr/X11R6/lib -lm -lpthread -lX11")
include_directories("${PROJECT_SOURCE_DIR}")
add_executable(net advanced.cpp)
Use target_link_libraries instead of adding CMAKE_EXE_LINKER_FLAGS.
And use link_directories instead of -L option in linker flags.

Impossible to link SDL/GLUT/GLM Ubuntu [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I actualy work on a openGL projet on my VM but i need openGL 4.5 so i have install ubuntu on my laptop with (GTX 870M) who is compatible 4.5 (i check with glxinfo). But my problem is after install gcc, build-essential, libglew-dev, freeglut3-dev, freeglut3 and SDL2. I can't make my projet i have error like undefined reference on « SDL_WasInit » , « glBegin », .... for all library installed...
i try with makefile like :
ifeq "$(shell uname)" "Darwin"
LIBGL= -framework OpenGL
else
LIBGL= -lGLU -lGL
endif
CXXFLAGS += `pkg-config glew --cflags` `sdl2-config --cflags` -g -W -Wall -Wno-unused-parameter -Wno-deprecated-declarations
LDFLAGS += `pkg-config glew --libs` `sdl2-config --libs` $(LIBGL)
all : main.exe
run : main.exe
./main.exe
main.exe : main.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o$# main.cpp
sol : main_solution.exe
runs : main_solution.exe
./main_solution.exe
main_solution.exe : main_solution.cpp *.h
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $# main_solution.cpp
clean :
rm -f *.o *.exe
and with cmakefile like :
cmake_minimum_required(VERSION 3.3)
project(src)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lglut -lGLU -lGL -lGLEW -lm -lSDL2 -lSDL2main -Wall -g")
set(SOURCE_FILES
"""ALL SOURCE FILES"""")
add_executable(src ${SOURCE_FILES})
This projet (cmakefile and makefile) work fine on my virtual machine ...
I hope you can help me thx.
In CMake you use target_link_libraries to specify which libraries to use. In general you want to use find_package(…) to locate the configuration for a specific library, then use the variables introduced by it to link.
find_package(OpenGL)
add_executable(foo …)
target_link_libraries(foo ${OPENGL_gl_LIBRARY} …)
You should look into the cmake modules that are used by find_package to see what the names of the variables they configure are.

Qt creator -1: error: [CMakeFiles/yahtzee.dir/gamecontroller.cpp.o] Error 1 : File not found

I am using Qt creator 2.5 with CMake (2.8.7) and gcc 4.6.3 and lately I have encountered this strange error :
:-1: error: [CMakeFiles/yahtzee.dir/gamecontroller.cpp.o] Error 1
File not found
What can I do about it ? CMake is not generating this gamecontroller.cpp.o
this is my CMakeLists.txt file
project(cpp_workshop)
cmake_minimum_required(VERSION 2.6)
# Set default compile flags for GCC
if(CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "GCC detected, adding compile flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pedantic -Wall -Wextra -Werror")
else()
message(STATUS "GCC not detected, probably running Windows")
endif(CMAKE_COMPILER_IS_GNUCXX)
#add_definitions("-Wall -Werror")
ADD_DEFINITIONS("-Wno-unused-parameter")
add_executable(yahtzee #name of the executable
gamecontroller.h gamecontroller.cpp
player.h player.cpp
game.h game.cpp
dice.h dice.cpp
strategy.cpp strategy.h
istrategy.h
game_rules.h
main singleton
)
And just to mention, all these files are located in directory and this is the 'build' directory that CMake creates :
CMakeCache.txt CMakeFiles cmake_install.cmake cpp_workshop.cbp Makefile
and the above CMakeFiles dir
CMakeCCompiler.cmake CMakeDirectoryInformation.cmake CompilerIdCXX TargetDirectories.txt
cmake.check_cache CMakeOutput.log Makefile2 yahtzee.dir
CMakeCXXCompiler.cmake CMakeSystem.cmake Makefile.cmake
CMakeDetermineCompilerABI_C.bin CMakeTmp Progress
CMakeDetermineCompilerABI_CXX.bin CompilerIdC progress.marks
Ok, the problem was with Cmake's argument passed to gcc
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
I changed that to
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
and now it works. Surprisingly, C++11 does not work with gcc yet ?

Resources