translate g++ code to clion environment - c++

I am new in clion. on gcc i always use:
g++ bin/obj/main.o -o bin/main -lboost_filesystem -lboost_system -lcrypto
How to do it in clion?
It seems my CMakeList does not work:
cmake_minimum_required(VERSION 3.1)
project(motion_simulation)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(motion_simulation ${SOURCE_FILES})
link_libraries(lboost_filesystem)
link_libraries(lboost_system)
link_libraries(lcrypto)

Try including the keyword "CMake" into your search next time. This question is actually not CLion specific because CLion actually uses CMake as buildsystem.
CMake is very well documented, and you should be able to find a lot of answers regarding your problem.
You could first try to get rid of that "l":
link_libraries(boost_filesystem)
If that doesn't work you should take a look how the find_package() command works. http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries
And here is a detailed explanation how to find Boost libs and include directory.
http://www.cmake.org/cmake/help/v3.0/module/FindBoost.html

As you are using CMake 3.1 you can use some more advanced features of CMake.
With CMAKE_CXX_STANDARD you can select which C++ version you want to use and CMake will select the corresponding compiler flags (see docs).
link_libraries is one possibility, but it has to be called before add_executable or add_library. The alternative is target_link_libraries which links only to a single target, but can also manage transitive dependencies (docs).
CMake comes with find_package modules for OpenSSL and Boost to find dependencies and with the option REQUIRED, you can ensure that they are found on the system. Boost also supports COMPONENTS to select which libraries you need.
In case you ever work on a system, where OpenSSL and Boost are not installed in /usr/, you can already use target_include_directories to specify where the headers for your executable is found. Like target_link_libraries, target_include_directories can work with transitive dependencies, in this case PRIVATE.
cmake_minimum_required(VERSION 3.1)
project(motion_simulation)
set(CMAKE_CXX_STANDARD 11)
find_package(Boost REQUIRED COMPONENTS filesystem system)
find_package(OpenSSL REQUIRED)
set(SOURCE_FILES main.cpp)
add_executable(motion_simulation ${SOURCE_FILES})
target_include_directories(motion_simulation PRIVATE ${Boost_INCLUDE_DIRS} ${OPENSSL_INCLUDE_DIR})
target_link_libraries( motion_simulation PRIVATE ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES})

Related

pybind11 and another libs in one project [duplicate]

I want to be able to call my C++ code as a python package. To do this I am using pybind11 with CMakelists (following this example https://github.com/pybind/cmake_example). My problem is that I have to include GSL libraries in the compilation of the code, and these need an explicit linker -lgsl .
If I were just to compile and run the C++ without wrapping it with python, the following Cmakelists.txt file does the job
cmake_minimum_required(VERSION 3.0)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
project(myProject)
add_executable(
myexecutable
main.cpp
function1.cpp
)
find_package(GSL REQUIRED)
target_link_libraries(myexecutable GSL::gsl GSL::gslcblas)
but when using pybind11 the template I found doesn't allow the add_executable therefore target_link_libraries doesn't work.
I have trie this
project(myProject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES) # See below (1)
# Set source directory
set(SOURCE_DIR "project")
# Tell CMake that headers are also in SOURCE_DIR
include_directories(${SOURCE_DIR})
set(SOURCES "${SOURCE_DIR}/functions.cpp")
# Generate Python module
add_subdirectory(lib/pybind11)
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
FIND_PACKAGE(GSL REQUIRED)
target_link_libraries(GSL::gsl GSL::gslcblas)
but this produces errors in the building.
Any idea ?
Function pybind11_add_module creates a library target, which can be used for link added module with other libraries:
pybind11_add_module(namr ${SOURCES} "${SOURCE_DIR}/bindings.cpp")
target_link_libraries(namr PUBLIC GSL::gsl GSL::gslcblas)
This is explicitely stated in documentation:
This function behaves very much like CMake’s builtin add_library (in fact, it’s a wrapper function around that command). It will add a library target called <name> to be built from the listed source files. In addition, it will take care of all the Python-specific compiler and linker flags as well as the OS- and Python-version-specific file extension. The produced target <name> can be further manipulated with regular CMake commands.

Boost Dynamic Linking

I am aware this question was asked many times before, always ending with the same answer... add -DBOOST_LOG_DYN_LINK to your CMakeLists.txt file.
However, I've had this in my cmake for a long time and everything was linking without any problems. Now I decided to switch to Ubuntu 18.04 from 16.04 and update my projects one by one....
This is my cmake file:
cmake_minimum_required(VERSION 2.8.4)
project(FOO)
find_package(Boost REQUIRED COMPONENTS system timer filesystem log program_options unit_test_framework)
find_package(CGAL COMPONENTS Core)
find_library(OPEN_MESH_CORE_LIBRARY OpenMeshCore /usr/local/lib/OpenMesh REQUIRED)
find_library(YAML_CPP_LIBRARY yaml-cpp REQUIRED)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)
link_libraries(${JSONCPP_LIBRARIES})
include_directories(${JSONCPP_INCLUDE_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math -fopenmp -msse2 -march=native -W -Wall -std=c++11")
add_definitions(-DBOOST_LOG_DYN_LINK=1)
add_definitions(-DUNIT_TEST_DATA="${CMAKE_SOURCE_DIR}/data")
include( ${CGAL_USE_FILE} )
include( CGAL_CreateSingleSourceCGALProgram )
set(SOURCE_FILES
Foo1.cpp
Foo2.cpp
Foo3.cpp)
add_executable(FOO main.cpp ${SOURCE_FILES})
target_include_directories(FOO PRIVATE ${JsonCpp_INCLUDE_DIRS})
target_link_libraries(FOO ${OPEN_MESH_CORE_LIBRARY} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARY} ${JSONCPP_LIBRARIES})
add_library(foo SHARED ${SOURCE_FILES})
target_link_libraries(foo ${OPEN_MESH_CORE_LIBRARY} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARY} ${JSONCPP_LIBRARIES})
add_executable(tests test.cpp ${SOURCE_FILES})
target_include_directories(FOO PRIVATE ${JsonCpp_INCLUDE_DIRS})
target_link_libraries(tests ${OPEN_MESH_CORE_LIBRARY} ${Boost_LIBRARIES} ${YAML_CPP_LIBRARY} ${JSONCPP_LIBRARIES})
When compiled, it fails with known long list of problems, all related ending with a message
undefined reference to boost::log::v2_mt_posix::...
I am not a cmake ninja and it is very likely that I am doing something wrong, however, I can not find out what it is.
EDIT:
I have successfully tried to build it on a clean Ubuntu 16.04 with gcc 5 and boost 1.58
I have unsuccessfully tried to build it on a clean 18.04 with gcc7.1
and boost 1.65
I have unsuccessfully tried to build it on a clean 18.04 with gcc 5.5
and self compiled boost 1.58
I have unsuccessfully tried to build it on a clean 18.04 with gcc 5.5
and self compiled boost 1.65
All attempts followed an exactly the same procedure.
Cause of this problem wasn't in Boost or CMakeLists.txt. It was actually a problem of CGAL calling find_package(Boost) again somewhere in their use protocol when include( ${CGAL_USE_FILE} ) was invoked, and ended up overriding Boost_LIBRARIES with links to it's own components, completely omitting what I've found previously.
Here is the reported issue
Solution is somewhat dirty as I'didn't patch CGAL up. All it took was changing the order when i call include( ${CGAL_USE_FILE} ) and placing it at the top.
find_package(CGAL COMPONENTS Core)
include( ${CGAL_USE_FILE} )
find_package(Boost REQUIRED COMPONENTS system timer filesystem log
program_options unit_test_framework)
Please NOTE that this is a quick fix and can result in further problems such as overriding Boost components required by CGAL!

CMake linking Boost: cannot find -lboost_program_options

I want to use Boosts support for command line flags in C++ on Linux. I use CMake to build the application, but I get a error "cannot find -lboost_program_options". The library boost_program_options was build independently using bjam, and the libraries are in the stage/lib subdirectory of the boost directory.
What does work: A solution is to add the stage/lib library using link_directories, but the CMake manual states:
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths.
So that should not be nescessary.
What I want to have working:
Using find_package should be enough, but that doesn't work, here's the CMakeLists:
cmake_minimum_required(VERSION 3.6)
project(inp_manipulation)
set(CMAKE_CXX_STANDARD 11)
include_directories(includes lib/boost_1_62_0 lib/)
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "lib/boost_1_62_0")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "lib/boost_1_62_0/stage/lib")
find_package(Boost 1.62.0)
include_directories(${Boost_INCLUDE_DIR})
file(GLOB SOURCES *.cpp)
set(MAIN_FILE main.cpp)
set(SOURCE_FILES ${SOURCES})
add_executable(inp_manipulation ${MAIN_FILE} ${SOURCE_FILES} )
target_link_libraries(inp_manipulation -static-libgcc -static-libstdc++ boost_program_options)
Question
Where is the mistake in the CMakeLists?
Thanks in advance!
First, you must tell cmake that you require the specific component library from boost:
find_package(Boost 1.62.0 COMPONENTS program_options)
Second, always use the output variables from BoostFind.cmake
target_link_libraries(inp_manipulation -static-libgcc -static-libstdc++ ${Boost_LIBRARIES})
Documentation here: https://cmake.org/cmake/help/v3.0/module/FindBoost.html
Output variables are:
Boost_FOUND - True if headers and requested libraries were found
Boost_INCLUDE_DIRS - Boost include directories
Boost_LIBRARY_DIRS - Link directories for Boost libraries
Boost_LIBRARIES - Boost component libraries to be linked
etc

Linking with cmake, icc and pthreads (linux)

I am trying to compile a simple test project with the Intel C++ Compiler, CMake, and std::threads support.
If I do :
icpc -std=c++11 -lpthread source/main.cpp
Then the build and the program work fine.
But with CMake I get an error:
CMake Error at CMakeLists.txt:21 (TARGET_LINK_LIBRARIES):
Cannot specify link libraries for target "test" which is not built by
this project.
CMakeLists.txt is:
project(test)
set (CMAKE_CXX_FLAGS "-std=c++11")
set(SOURCE_LIST "source/main.cpp")
TARGET_LINK_LIBRARIES(${PROJECT_NAME} pthread)
add_executable (${PROJECT_NAME} ${SOURCE_LIST})
I am building in an environment set by the script supplied by icc (compilervars.sh) and CMake is called with the -DCMAKE_C_COMPILER=icc -DCMAKE_CXX_COMPILER=icpc options. It works if I'm not using threads.
What is wrong with my use of CMake?
Thanks!
Looking back the documentation for target_link_libraries, a target must be declared before being used:
The named <target> must have been created in the current directory by a command such as add_executable() or add_library().
BTW as stated in this answer, you should not set CMAKE_CXX_FLAGS explicitly, and prefer setting CMAKE_CXX_STANDARD and related variables:
project(test)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(SOURCE_LIST "source/main.cpp")
add_executable(${PROJECT_NAME} ${SOURCE_LIST})
target_link_libraries(${PROJECT_NAME} pthread)
The order is wrong: first declare your executable and let CMake know that it exists
add_executable (${PROJECT_NAME} ${SOURCE_LIST})
and afterwards add the link libraries
TARGET_LINK_LIBRARIES(${PROJECT_NAME} pthread)
From target_link_libraries's documentation:
The named must have been created in the current directory by a command such as add_executable() or add_library()

What must I do with C++ libraries on cross compilation?

Here is my compiler part of my config:
IF(UNIX)
## Compiler flags
# specify the cross compiler
SET(CMAKE_C_COMPILER /home/username/projects/buildroot/output/host/usr/bin/arm-linux-gcc)
SET(CMAKE_CXX_COMPILER /home/username/projects/buildroot/output/host/usr/bin/arm-linux-g++)
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-O3")
set(CMAKE_EXE_LINKER_FLAGS "-lsqlite3 -lrt -lpthread")
endif()
target_link_libraries(complex
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY})
ENDIF(UNIX)
There are 3 problems : -lsqlite3 -lrt -lpthread
How must I to make them for my architecture and specify here? How to set (using set?) the path of compiled libraries after I will get them recompiled for my architecture somehow?
If you want to do cross-compilation with CMake you really should use a toolchain file for that. See the CMake Wiki for an introduction. In order to use third-party libraries (i.e. not included by the cross-compilation toolchain) you need to cross-compile them too.
Edit: Since you are using the buildroot toolchain, you can use the already included CMake toolchain file. Just pass -DCMAKE_TOOLCHAIN_FILE=/home/username/projects/buildroot/output/toolchainfile.cmake when invoking CMake. No need to set CMAKE_C_COMPILER and CMAKE_CXX_COMPILER in your CMakeLists.txt file. Also, setting CMAKE_CXX_FLAGS and CMAKE_EXE_LINKER_FLAGS is considered to be very bad practice.
Presumably you have built sqlite3 while building the buildroot toolchain, so you can use it just like any other library. I.e:
find_path(SQLITE3_INCLUDE_DIR sqlite3.h)
find_library(SQLITE3_LIBRARY sqlite3)
if(NOT SQLITE3_INCLUDE_DIR)
message(SEND_ERROR "Failed to find sqlite3.h")
endif()
if(NOT SQLITE3_LIBRARY)
message(SEND_ERROR "Failed to find the sqlite3 library")
endif()
find_package(Threads REQUIRED)
# ...
target_link_libraries(complex
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${SQLITE3_LIBRARY}
${CMAKE_THREAD_LIBS_INIT}
rt)
Lastly, do not set CMAKE_CXX_FLAGS to -O3. The user should pass -DCMAKE_BUILD_TYPE=Release when configuring the project instead.
You'll have to cross-compile the dependencies as well. The path depends on where you install them.
Btw., using -lpthread is not the safe way of getting POSIX threads. You should give the option -pthread to both the compiler and the linker.