CMake unable to link libxml2 in CLion - c++

tl;dr add set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" ) to CMakeLists.txt (more infos below).
I want to run a very simple application within CLion. This application uses libxml2.
I use CLion 2018.1.2 on Ubuntu 18.04 LTS, libxml2-dev is properly installed.
My project does compile with:
g++ -I /usr/include/libxml2 xmlTest.cpp -o xmlTest.exe -lxml2
it does not compile in CLion though, using this CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(xmlTest)
set(CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2 -lxml2 -lz -lm")
include_directories( /usr/local/include/libxml2 )
add_executable (xmlTest.exe xmlTest.cpp)
the compiler throws me:
/home/xxxx/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/181.4668.70/bin/cmake/bin/cmake --build /home/xxxx/src/cmake-build-debug --target xmlTest.exe -- -I /usr/include/libxml2 -lxml2
Scanning dependencies of target xmlTest.exe
[ 50%] Building CXX object CMakeFiles/xmlTest.exe.dir/xmlTest.cpp.o
[100%] Linking CXX executable xmlTest.exe
CMakeFiles/xmlTest.exe.dir/xmlTest.cpp.o: In function `main':
/home/xxxx/src/xmlTest.cpp:10: undefined reference to `xmlReadFile'
/home/xxxx/src/xmlTest.cpp:17: undefined reference to `xmlDocGetRootElement'
/home/xxxx/src/xmlTest.cpp:21: undefined reference to `xmlFreeDoc'
/home/xxxx/src/xmlTest.cpp:25: undefined reference to `xmlStrcmp'
/home/xxxx/src/xmlTest.cpp:27: undefined reference to `xmlFreeDoc'
my application (xmlTest.cpp):
#include <stdlib.h>
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
int main(int argc, char const *argv[]) {
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlReadFile(argv[1], NULL, 0);
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return 1;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return 1;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "users")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return 1;
}
return 0;
}
after fiddling with libxml2 and CMake for hours now without success, I am open to any idea what to do to tell CMake libxml2 actually exists...
Update #1:
thanks to #vre's suggestion I updated my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.10)
project(xmlTest)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2 -lxml2 -lz -lm")
set(CMAKE_CXX "/usr/local/include/libxml2")
find_library(libxml2 libxml2)
link_directories(/usr/local/include/libxml2)
add_compile_options("-I/usr/include/libxml2 -lxml2 -lz -lm")
find_package(LibXml2 REQUIRED)
add_executable (xmlTest.exe xmlTest.cpp)
target_compile_definitions(xmlTest.exe PUBLIC ${LibXml2_DEFINITIONS})
target_include_directories(xmlTest.exe PUBLIC ${LibXml2_INCLUDE_DIRS})
target_link_libraries(xmlTest.exe ${LibXml2_LIBRARIES})
This did not change or resolve the error though.
Update #2 (solution):
this is how to build projects with libxml2 in CLion:
cmake_minimum_required(VERSION 3.10)
project(xmlTest)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -I/usr/include/libxml2")
set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" )
add_executable (xmlTest.exe xmlTest.cpp)
My issue was that I had the wrong order of arguments in my CMAKE_CXX_FLAGS. This article points this out. Using set(CMAKE_CXX_STANDARD_LIBRARIES "-lxml2" ) was the key (see this)
I hope this helps some future timetravelers:

The solution you came up with works but the intention of CMake is to provide means to express a platform, compiler, and build tool independent way of describing projects. All the magic happens in the FindLibXml2.cmake module. And a huge number of modules are delivered ready to use with your CMake installation. See the modules directory under /usr/share/cmake-3.10/Modules on Ubuntu 18.04.
If you reformulate your CMakeLists.txt the following way:
cmake_minimum_required(VERSION 3.10)
project(xmlTest)
set(CMAKE_CXX_STANDARD 11)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
elseif(MSVC)
# add options for Visual C/C++ Compiler here
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
find_package(LibXml2 REQUIRED)
add_executable(xmlTest.exe xmlTest.cpp)
target_compile_definitions(xmlTest.exe PUBLIC ${LIBXML2_DEFINITIONS})
target_include_directories(xmlTest.exe PUBLIC ${LIBXML2_INCLUDE_DIRS})
target_link_libraries(xmlTest.exe ${LIBXML2_LIBRARIES})
you are able to work on a wide range of platforms and combinations of compilers and IDEs. This was tested on Ubuntu 18.04 with GCC 7.3 and Windows 10 with MSVC 15. You need some more steps to build on Windows (i.e. installing and setup libxml2 and all of its dependencies). And this should work with CLion too out of the box.

Related

How to fix: 'can not be used when making a shared object; recompile with -fPIC' using Cmake. Using plain g++ works

I get a message 'can not be used when making a shared object; recompile with -fPIC'
I have try other examples and the issue is the same.
I have try
changing from MODULE to SHARED
cmake .. -DCMAKE_CXX_FLAGS=-fPIC
and other variations
this works:
c++ -c -fPIC -I/usr/include/python3.6m ../account.cpp
c++ -shared -Wall -Werror -Wl,--export-dynamic account.o -L/usr/local/lib -lboost_python36 -o account.so
Here is the basic cmake
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Using: make VERBOSE=1 the output commands are:
c++ -DBOOST_ALL_NO_LIB -Daccount_EXPORTS -I/usr/include/python3.6m -isystem /usr/local/include -fPIC -o CMakeFiles/account.dir/account.cpp.o -c /src/boost_python_example/account.cpp
c++ -fPIC -shared -Wl,-soname,account.so -o account.so CMakeFiles/account.dir/account.cpp.o /usr/local/lib/libboost_python36.a
So the cmake is not getting the same paths and flags, I am learning cmake so Im trying to understand this problem. Clearly the problem is not on actual libs but telling cmake where to find the proper ones.
The solution was quite simple. Comparing both commands what was missing on the cmake command was: --export-dynamic
So I solved using option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON) interesting enough the comment is needed.
Working solution:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Thanks everyone for the comments they lead me to a solution

Build project with "experimental/filesystem" using cmake

I need to add a "experimental/filesystem" header to my project
#include <experimental/filesystem>
int main() {
auto path = std::experimental::filesystem::current_path();
return 0;
}
So I used -lstdc++fs flag and linked with libstdc++fs.a
cmake_minimum_required(VERSION 3.7)
project(testcpp)
set(CMAKE_CXX_FLAGS "-std=c++14 -lstdc++fs" )
set(SOURCE_FILES main.cpp)
target_link_libraries(${PROJECT_NAME} /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++fs.a)
add_executable(testcpp ${SOURCE_FILES})
However, I have next error:
CMake Error at CMakeLists.txt:9 (target_link_libraries): Cannot
specify link libraries for target "testcpp" which is not built by
this project.
But if I compile directly, it`s OK:
g++-7 -std=c++14 -lstdc++fs -c main.cpp -o main.o
g++-7 -o main main.o /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++fs.a
Where is my mistake?
It's just that the target_link_libraries() call has to come after the add_executable() call. Otherwise the testcpp target is not known yet. CMake parses everything sequential.
So just for completeness, here is a working version of your example I've tested:
cmake_minimum_required(VERSION 3.7)
project(testcpp)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# NOTE: The following would add library with absolute path
# Which is bad for your projects cross-platform capabilities
# Just let the linker search for it
#add_library(stdc++fs UNKNOWN IMPORTED)
#set_property(TARGET stdc++fs PROPERTY IMPORTED_LOCATION "/usr/lib/gcc/x86_64-linux-gnu/7/libstdc++fs.a")
set(SOURCE_FILES main.cpp)
add_executable(testcpp ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} stdc++fs)

CMake can't find Thrift libraries

I'm trying to build my C++ app using CMake (CLion on Windows). Application uses Thrift libraries, and those have been successfully (at least I hope so) built with Microsoft Visual 2015. I can built app with SCons:
import os
from os import path, listdir
gen_cpp = [path.join('gen-cpp', f) for f in listdir('gen-cpp') if f.endswith('.cpp')]
client_source = [path.join('logic', folder, f) for folder in ['', 'parser', 'mars', 'view']
for f in listdir(path.join('logic', folder)) if f.endswith('.cpp')]
server_source = [path.join('server', 'server.cpp')]
tests_source = [path.join('test_cases', f) for f in listdir('test_cases') if f.endswith('.cpp')]
cpppath = ['.','c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\src\\','c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\src\\thrift\\server',
'c:\\Users\\Antek\\libs\\boost_1_64_0\\boost\\']
libpath = ['C:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\x64\\Release\\', 'c:\\Users\\Antek\\libs\\boost_1_64_0\\boost\\stage\\x64\\lib\\',
'C:\\OpenSSL-Win64\\lib']
libs = ['libthrift','libssl','openssl','libcrypto']
env = Environment(CPPPATH = cpppath,
LIBS = libs,
LIBPATH = libpath,
MSVC_VERSION='14.0',
CPPFLAGS='/EHsc',
)
gen_cpp_o = env.Object(gen_cpp)
client_o = env.Object(client_source)
tests_o = env.Object(tests_source)
tests_files = gen_cpp_o + [f for f in client_o if str(f) != path.join('logic', 'main.obj')] + tests_o
env.Program('CoreWars', gen_cpp_o + client_o)
env.Program('Server', gen_cpp_o + server_source)
env.Program('tests', tests_files)
.exe files, created as a result of the 'scons' command, work perfectly fine. But, when I try to do build app with CMake, like that:
cmake_minimum_required(VERSION 3.7)
project(client)
SET (THRIFT_ROOT "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0")
SET (THRIFT_INCLUDEDIR "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\src")
SET (THRIFT_LIBRARYDIR "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\x64\\Release")
include_directories(${THRIFT_INCLUDEDIR})
MESSAGE("Thrift_LIBRARIES: ${THRIFT_LIBRARYDIR}")
MESSAGE("Thrift_INCLUDES: ${THRIFT_INCLUDEDIR}")
#find_package(Thrift REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET (BOOST_ROOT "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost")
SET (BOOST_INCLUDEDIR "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost")
SET (BOOST_LIBRARYDIR "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost\\libs")
find_package(Boost 1.64.0 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
set(SOURCE_FILES ../gen-cpp/MARS.cpp ../gen-cpp/mars_constants.cpp ../gen-cpp/mars_types.cpp main.cpp
parser/InstructionFactory.cpp parser/RedcodeParser.cpp parser/InstructionData.cpp ServerConnector.cpp
mars/Instruction.cpp mars/InstructionOperator.cpp parser/ParserException.cpp MainController.cpp
mars/MarsSimulator.cpp Initializer.cpp Player.cpp Player.h PlayerInfo.cpp PlayerInfo.h Warrior.cpp
Warrior.h PlayerCreator.cpp PlayerCreator.h view/ViewInput.cpp view/ViewInput.h MarsResult.cpp MarsResult.h
mars/DatInstruction.cpp mars/DatInstruction.h mars/MovInstruction.cpp mars/MovInstruction.h)
add_executable(CoreWars ${SOURCE_FILES})
target_link_libraries(CoreWars ${Boost_LIBRARIES} $(THRIFT_LIBRARYDIR))
CMake reports an error:
[ 4%] Linking CXX executable CoreWars.exe
G__~1.EXE: error: $(THRIFT_LIBRARYDIR): No such file or directory
mingw32-make.exe[3]: *** [logic/CoreWars.exe] Error 1
If i try to find thrift library with find_package(Thrift REQUIRED) command, I get the following error:
CMake Error at logic/CMakeLists.txt:13 (find_package):
By not providing "FindThrift.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Thrift", but
CMake did not find one.
Could not find a package configuration file provided by "Thrift" with any
of the following names:
ThriftConfig.cmake
thrift-config.cmake
I would be grateful for any help.
EDIT1:
I tried to solve my problem with the help of this question: CMake link to external library, but, unfortunately, it does not work for me.
I have edited my CMakeList.txt:
cmake_minimum_required(VERSION 3.7)
project(client)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET (THRIFT_ROOT "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0")
SET (THRIFT_INCLUDEDIR "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\src")
SET (THRIFT_LIBRARYDIR "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\x64\\Release")
MESSAGE("Thrift_LIBRARIES: ${THRIFT_LIBRARYDIR}")
MESSAGE("Thrift_INCLUDES: ${THRIFT_INCLUDEDIR}")
find_library(THRIFT_FOUND_LIB thrift PATHS ${THRIFT_LIBRARYDIR})
MESSAGE("Thrift found lib: ${THRIFT_FOUND_LIB}")
link_libraries(thrift "${THRIFT_FOUND_LIB}")
link_directories(${THRIFT_LIBRARYDIR})
include_directories(${THRIFT_INCLUDEDIR})
SET (BOOST_ROOT "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost")
SET (BOOST_INCLUDEDIR "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost")
SET (BOOST_LIBRARYDIR "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost\\libs")
find_package(Boost 1.64.0 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
set(SOURCE_FILES ../gen-cpp/MARS.cpp ../gen-cpp/mars_constants.cpp ../gen-cpp/mars_types.cpp main.cpp
parser/InstructionFactory.cpp parser/RedcodeParser.cpp parser/InstructionData.cpp ServerConnector.cpp
mars/Instruction.cpp mars/InstructionOperator.cpp parser/ParserException.cpp MainController.cpp
mars/MarsSimulator.cpp Initializer.cpp Player.cpp Player.h PlayerInfo.cpp PlayerInfo.h Warrior.cpp
Warrior.h PlayerCreator.cpp PlayerCreator.h view/ViewInput.cpp view/ViewInput.h MarsResult.cpp MarsResult.h
mars/DatInstruction.cpp mars/DatInstruction.h mars/MovInstruction.cpp mars/MovInstruction.h)
add_executable(CoreWars ${SOURCE_FILES})
MESSAGE("Cmake prefix path: ${CMAKE_PREFIX_PATH}")
LINK_DIRECTORIES(${CMAKE_BINARY_DIR})
target_link_libraries(CoreWars ${Boost_LIBRARIES} thrift)
Cmake commnad find_library successfully finds my thrift. Here is CMake output:
C:\Users\Antek\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\171.4073.41\bin\cmake\bin\cmake.exe -DCMAKE_BUILD_TYPE=Release -G "CodeBlocks - MinGW Makefiles" C:\Users\Antek\Documents\MEGAsync\_STUDIA\ZPR\proj1\Core-Wars-ZPR
Thrift_LIBRARIES: c:\Users\Antek\libs\thrift-0.10.0\thrift-0.10.0\lib\cpp\x64\Release
Thrift_INCLUDES: c:\Users\Antek\libs\thrift-0.10.0\thrift-0.10.0\lib\cpp\src
Thrift found lib: C:/Users/Antek/libs/thrift-0.10.0/thrift-0.10.0/lib/cpp/x64/Release/libthrift.lib
-- Boost version: 1.64.0
Cmake prefix path:
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Antek/Documents/MEGAsync/_STUDIA/ZPR/proj1/Core-Wars-ZPR/cmake-build-release
But, when I try to link my project, there is an error:
[ 4%] Linking CXX executable CoreWars.exe
C:/PROGRA~1/MINGW-~1/X86_64~1.2-P/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lthrift
Am I missing something? Note that thrift has been built in x64/Release mode, and I am using x64 mingw in CLion and CMake is also in release mode.
Thanks.
EDIT2:
After fiew improvements, linker seems to be able to find thrift. But, now, I get undefined reference to... error. For the thrift and also some boost libs. For example:
CMakeFiles\CoreWars.dir/objects.a(MARS.cpp.obj):MARS.cpp:(.text+0x454): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const'
and
CMakeFiles\CoreWars.dir/objects.a(MARS.cpp.obj):MARS.cpp:(.text.startup+0x11): undefined reference to `boost::system::generic_category()'
What may be the cause of this error? Shouldn't ${Boost_LIBRARIES} include also Boost.System library path? Googled solutions seem to not be appropriate in this case.
Updated CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
project(client)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET (THRIFT_ROOT "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0")
SET (THRIFT_INCLUDE_DIR "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\src")
SET (THRIFT_LIB_DIR "c:\\Users\\Antek\\libs\\thrift-0.10.0\\thrift-0.10.0\\lib\\cpp\\x64\\Release")
find_library(THRIFT_FOUND_LIB thrift PATHS ${THRIFT_LIB_DIR})
MESSAGE("Found Thrift lib: ${THRIFT_FOUND_LIB}")
find_path(THRIFT_FOUND_HEADERS thrift PATHS ${THRIFT_INCLUDE_DIR})
MESSAGE("Found Thrift headers: ${THRIFT_FOUND_HEADERS}")
include_directories(${THRIFT_FOUND_HEADERS})
link_directories(${THRIFT_FOUND_HEADERS})
link_directories(${THRIFT_FOUND_LIB})
SET (BOOST_ROOT "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost")
#SET (BOOST_INCLUDEDIR "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost")
#SET (BOOST_LIBRARYDIR "c:\\Users\\Antek\\libs\\boost_1_64_0\\boost\\libs")
find_package(Boost 1.64.0 REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARIES})
set(SOURCE_FILES ../gen-cpp/MARS.cpp ../gen-cpp/mars_constants.cpp ../gen-cpp/mars_types.cpp main.cpp
parser/InstructionFactory.cpp parser/RedcodeParser.cpp parser/InstructionData.cpp ServerConnector.cpp
mars/Instruction.cpp mars/InstructionOperator.cpp parser/ParserException.cpp MainController.cpp
mars/MarsSimulator.cpp Initializer.cpp Player.cpp Player.h PlayerInfo.cpp PlayerInfo.h Warrior.cpp
Warrior.h PlayerCreator.cpp PlayerCreator.h view/ViewInput.cpp view/ViewInput.h MarsResult.cpp MarsResult.h
mars/DatInstruction.cpp mars/DatInstruction.h mars/MovInstruction.cpp mars/MovInstruction.h)
add_executable(CoreWars ${SOURCE_FILES})
target_include_directories(CoreWars SYSTEM PUBLIC ${THRIFT_FOUND_HEADERS})
target_link_libraries(CoreWars LINK_PUBLIC ${THRIFT_FOUND_LIB} ${Boost_LIBRARIES} )
Thank You again.
It looks like you are passing the directory where to look for the library, but not the name of the actual library. Add this in your CMakelists.txt, it should work:
target_include_directories(${THRIFT_INCLUDEDIR})
target_link_libraries(CoreWars ${Boost_LIBRARIES} libthrift.lib) # Or may be just 'thrift'

JetBrains CLion can not suggest member methods of string

I'm new to JetBrains' CLion. Sorry if my question is basic but I didn't find any answer by searching.
#include <string>
int main() {
std::string str;
str.assign("ABC");
}
This very simple code compiles fine but the problem is the editor can't suggest member methods for str.
I'm using CLion 2016.3.2 on Fedora 25
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(ITP)
set(CMAKE_CXX_STANDARD "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(/usr/include /usr/local/include /usr/local/pgsql/include)
set(SOURCE_FILES
main.cpp
commander.cpp
)
add_custom_target(ITP command make -C /home/ben/projects/ITP)
Note: when I point to #include the editor didn't knows relevant include file!
Finally I reached to an answer!
Clion works with Cmake. there for CMakeLists.txt is very important.
many settings with cmake can effect code compilation, build types and ...
for more details please see my new CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(ITP)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
set(SOURCE_FILES
main.cpp
commander.cpp)
link_directories(/usr/local/pgsql/lib/)
message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
message("C Flags = ${CMAKE_CXX_FLAGS_DEBUG}")
add_executable(itp OrderManagementSystem/main.cpp)
add_custom_command(
TARGET itp POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_SOURCE_DIR}/config.ini
${CMAKE_CURRENT_BINARY_DIR})
TARGET_LINK_LIBRARIES( itp
pthread
boost_system boost_thread
protobuf
zmq
pq
)
I'm new with both Clion and cmake. but these two ..

undefined reference to boost::iostreams::mapped_file_source::init() using CMake

Linking error on minimal example with Boost Iostreams. Looks like I have not linked with libboost_iostream, however CMake reports that library is found and other applications with Boost compile and link without any problems.
Using Cmake for build:
cmake_minimum_required(VERSION 3.0)
project(mmap_example CXX)
set(TARGET mmap_example)
set(BOOST_MIN_VERSION "1.61.0")
set(Boost_ADDITIONAL_VERSIONS "1.61.0" "1.61")
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_ROOT ${MY_BOOST_DIR})
find_package(Boost ${BOOST_MIN_VERSION} COMPONENTS iostreams REQUIRED)
set(CMAKE_CXX_FLAGS "-std=c++11 -std=gnu++1y -pthread")
set(CMAKE_EXE_LINKER_FLAGS "-std=c++11 -std=gnu++1y -pthread")
file(GLOB SOURCES *.cpp)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(${TARGET} ${SOURCES})
target_link_libraries(${TARGET} ${Boost_IOSTREAMS})
C++ itself:
#include <boost/iostreams/device/mapped_file.hpp>
namespace boost_io = boost::iostreams;
int main(int argc, char** argv) {
boost_io::mapped_file_source file(argv[1]);
return 0;
}
GCC output:
Linking CXX executable mmap_example
CMakeFiles/mmap_example.dir/mmap.cpp.o: In function boost::iostreams::mapped_file_source::mapped_file_source<char*>(char* const&, unsigned int, long long):
mmap.cpp:(.text._ZN5boost9iostreams18mapped_file_sourceC2IPcEERKT_jx[_ZN5boost9iostreams18mapped_file_sourceC5IPcEERKT_jx]+0x43): undefined reference to boost::iostreams::mapped_file_source::init()
gcc (Debian 4.9.2-10) 4.9.2
Cmake 3.0.2
Boost 1.61
I'm not sure that ${Boost_IOSTREAMS} is the correct variable to use, AFAIK it should be ${Boost_LIBRARIES} (at least that is what I always use).
You can check if the variable is really set by using
message(STATUS "Boost_IOSTREAMS: ${Boost_IOSTREAMS}")
in your cmake file.
You can also use
make all VERBOSE=1
to list all the commands, to check what libraries are present on the linker command line.