I know how the basic Makefile for a simple project using pjsip library looks like. But what does the equivalent CMake file look like?
project(myapp)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
include (FindPkgConfig)
if (PKG_CONFIG_FOUND) # true if pkg-config works on the system
pkg_check_modules(PJSIP REQUIRED libpjproject)
endif()
include_directories(${PJSIP_INCLUDE_DIRS})
link_directories(${PJSIP_LIBRARY_DIRS})
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${PJSIP_LIBRARIES})
Related
I've build my unit test code on Ubuntu 21.10, CMake 3.18.4 and GTest 1.10.0.20201025-1.1.
I wrote CMakeList.txt file as this.
# The minimum version of CMake Required
cmake_minimum_required (VERSION 2.8.12)
# Any project name will suffice, this has connotaions when using advanced CMake Features
set(PROJECT_NAME tests)
project (${PROJECT_NAME})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Just in case someone had the include files in seperate directory
include_directories(../include)
include_directories(..)
# This uses the complete student's library
aux_source_directory(.. SRC_LIST)
list(REMOVE_ITEM SRC_LIST "../main.cpp")
message ( STATUS "Compiling test_lib with following files ${SRC_LIST}" )
add_library(test_lib ${SRC_LIST})
# Now we make the gtests
set(GTEST_ROOT "/usr/src/gtest" CACHE PATH "Path to googletest")
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
if(NOT GTEST_LIBRARY)
message("GTest library not found")
endif()
add_executable(rawTests test_rawdata.cpp)
target_link_libraries(rawTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(rawTests test_lib)
add_executable(timeTests test_time.cpp)
target_link_libraries(timeTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(timeTests test_lib)
It works properly on my end.
But when I deliver this to my friend who uses CMake 3.22.4, it throws error look like this
Error Image
It's kind of weird issue and I didn't ever faced this sort of issue before.
I wonder anybody who has deep knowledge for CMake and GTest can help me to handle this.
Thank you in advance.
Let's say I wanted to develop a simple library with the following structure:
My CMakeLists.txt then looks like this:
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 14)
set(INC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include/")
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/")
file(GLOB INCLUDES "${INC_DIR}/httplib/*.hpp")
file(GLOB SOURCES "${SRC_DIR}/*.cpp")
project(httplib)
add_library(httplib STATIC ${INCLUDES} ${SOURCES})
include_directories(${INC_DIR})
This works fine but what is the usual way to configure CLion projects and CMake in order to test a library effectively ?
I used this link to install OpenCV.
What works:
1.OpenCV works fine with python (running from terminal).
2.I can import opencv libraries in a single C++ program.
What does not work :
When the code is spread across multiple and you need to build it using CMake.
Here's my CmakeLists.txt :
1.cmake_minimum_required(VERSION 3.9)
2.project(Image_processing)
3.set(CMAKE_CXX_STANDARD 14)
4.find_package(OpenCV REQUIRED)
5.include_directories(/home/user/opencv/build)
6.add_executable(main main.cpp)
7.target_link_libraries(project_name ${OpenCV_LIBS})
Errors (can regenerate them by commenting lines 4,5 and 7 in above CMake file):
undefined reference to OpenCV functions.
CMake Error at CMakeLists.txt:7 (target_link_libraries):
Cannot specify link libraries for target "Image_processing" which is not
built by this project.
Correct it with:
cmake_minimum_required(VERSION 3.5)
project(Image_processing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
In your CMakeLists.txt, the exe-name is not matching with the target-link-name. I modify the line, then it works on my PC.
The CMakeLists.txt of an OpenCV Project:
cmake_minimum_required(VERSION 3.5)
project(Image_processing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
#include_directories(/home/user/opencv/build)
add_executable(Image_processing main.cpp)
target_link_libraries(Image_processing ${OpenCV_LIBS})
I am new to C++ and have just started to use Cmake for linking the libraries to my project. I need to use a library:
https://github.com/Gnimuc/FastPD
Fortunately, I managed to build the library using Cmake (in my build there is no *.lib file at all), but I don't know how to link it to my project. I mean that I don't know how to add it to my cmakelists.txt :
(PS. I'm also using two other libraries ITK and VTK; but I can't link the above mentioned library to my project or main.cpp.)
################################################
cmake_minimum_required(VERSION 2.8)
project(My_project)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
add_executable(My_project MACOSX_BUNDLE main.cpp)
target_link_libraries(My_project
${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES})
################################################
Thanks in advance for your helps,
Assuming you installed the library and its header files, you can then search for the header files using find_path, and add the found path to the include directories. Then you can search for the library by using find_library, and add the library with the target_link_libraries command.
I tried both shared and static types for creation of the library. In the case of shared, no *.lib file was created, and in the case of static, my project couldn't link to the library. Therefore, in my project's CmakeLists.txt, I decided to add all the *.cpp and headers as libraries and then link them together (as I didn't know the dependency among them!!!), and finally, I linked them to my project. Perhaps it does not make sense but it works; hope it helps you:
CmakeLists.txt
##############################################
cmake_minimum_required(VERSION 2.6)
set(PROJ_NAME PROJECT46)
PROJECT(${PROJ_NAME})
# Prevent compilation in-source
if( ${CMAKE_BINARY_DIR} STREQUAL ${PROJECT_SOURCE_DIR} )
Message( " " )
Message( FATAL_ERROR "Source and build directories are the same.
Create an empty build directory,
change into it and re-invoke cmake")
endif()
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support.
Please use a different C++ compiler.")
endif()
##############################################
## External libraries
##############################################
list( APPEND CMAKE_MODULE_PATH
${PROJECT_SOURCE_DIR}/cmake
)
# Blitz
find_package( Blitz++ REQUIRED )
list( APPEND PROJ_INCLUDE_DIRS
${Blitz++_INCLUDE_DIR}
)
list( APPEND
PROJ_LIB
${Blitz++_LIBRARIES}
)
# ITK and VTK
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
else()
find_package(ItkVtkGlue REQUIRED)
include(${ItkVtkGlue_USE_FILE})
set(Glue ItkVtkGlue)
endif()
##############################################
# FASTPD
add_library(FASTPD Fast_PD.cpp Fast_PD.h common.h block.h)
add_library(GRAPH graph.h graph.cpp)
add_library(linked LinkedBlockList.h LinkedBlockList.cpp)
add_library(MAXFLOW maxflow.cpp)
include_directories(${PROJ_INCLUDE_DIRS})
add_executable(${PROJ_NAME} main.cpp)
target_link_libraries(linked MAXFLOW)
target_link_libraries(GRAPH linked)
target_link_libraries(FASTPD GRAPH)
target_link_libraries(${PROJ_NAME} FASTPD)
target_link_libraries(${PROJ_NAME}
${PROJ_LIB} ${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES}
)
I'm tring to use CMS and cannot link libraries that i compiled on my linux. My CMakeLists.txt:
project(region_algorithm)
SET_TARGET_PROPERTIES(PROPERTIES LINKER_LANGUAGE CXX)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
INCLUDE_DIRECTORIES(
/usr/local/include/activemq-cpp-3.8.3
/usr/local/apr/include/apr-1)
link_libraries(
/usr/local/lib/libactivemq-cpp.a
/usr/local/apr/lib/libapr-1.a)
When i'm building project i got errors like:
regionalgorithmconsumer.cpp:-1: error: undefined reference to
`decaf::util::concurrent::CountDownLatch::CountDownLatch(int)'