CMake OBJECT file flags not working - c++

I am trying to make a simple cmake file to generate a makefile for my project.
Now, i have to first genereate some .o files and then compile the main script.
I use some std 11 functions so i need to define that flag.
My CMakeList.txt file looks something like this:
cmake_minimum_required(VERSION 2.8)
project(P)
#C++ compiler
set(CMAKE_CXX_COMPILER_INIT g++)
#Compiler flags config
set(CMAKE_CXX_FLAGS "-W")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS "-I../..")
set(CMAKE_CXX_FLAGS "-lpthread")
set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_CXX_FLAGS "-Iinclude/")
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "-lz")
set(CMAKE_CXX_FLAGS "-lrt")
set(CMAKE_CXX_STANDARD 11)
set_source_files_properties(a.c PROPERTIES LANGUAGE CXX)
add_library(A OBJECT a.h a.c)
set_source_files_properties(b.cpp PROPERTIES LANGUAGE CXX)
add_library(B OBJECT b.h b.cpp)
add_executable(P p.cpp $<TARGET_OBJECTS:A> $<TARGET_OBJECTS:B>)
Now, what happens is that when i run the makefile, the compiler gives me an error with b.cpp.It says that one of the functions is not defined and i know that that error is because is not compiling with standard 11.
How can i tell cmake to add std=c++11 flag when compiling object files?

I know what was wrong. Apparently you can't add the flags in different lines. I should've done something like:
set(CMAKE_CXX_FLAGS "-W -Wall -I../.. -lpthread -g -Iinclude -std=c++11 -lz -lrt")

Related

Visual Studio Code debugger doesn't show the contents of variables

I'm using Visual Studio Code on Ubuntu 18.04 to write some C++ code using external libraries. I'm not sure why, but whenever I run the debugger, the contents of most variables doesn't show.
I've attached an example of this issue.
Why is this happening?
Edit:
cmake_minimum_required(VERSION 3.10.2)
project(Proj)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_CXX_COMPILER /usr/bin/g++)
set(ARENA_DIR "~/Arena")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CC "g++")
set(CFLAGS "-Wall -g -O2 -std=c++14 -Wno-unknown-pragmas")
add_definitions(${CFLAGS})
set(RM "rm -f")
include_directories(${ARENA_DIR}include/Arena)
include_directories(${ARENA_DIR}include/GenTL)
include_directories(${ARENA_DIR}include/Save)
include_directories(${ARENA_DIR}GenICam/library/CPP/include)
...
set(ARENA_DEBUG_LIBS
${ARENA_DIR}libarenad.so
${ARENA_DIR}libsaved.so
${ARENA_DIR}libgentld.so)
set(ARENA_RELEASE_LIBS
${ARENA_DIR}libarena.so
${ARENA_DIR}libsave.so
${ARENA_DIR}libgentl.so)
set(LIBS
${GENICAMLIBS}
${FFMPEGLIBS}
"/usr/lib/x86_64-linux-gnu/libpthread.so")
add_executable(main main.cpp)
set_target_properties(main PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
target_link_libraries(main ${LIBS} debug ${ARENA_DEBUG_LIBS})
target_link_libraries(main ${LIBS} optimized ${ARENA_RELEASE_LIBS})
Edit 2:
After changing the g++ flag to -O0 instead of -O2, I get this result:
But I would expect to be able to see the object's properties.

Dynamic link error cmake

When I compile my code using command line everything works fine:
g++ main.cpp -lpngwriter
But when I try using cmake I get undefined reference errors.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(myproject)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpngwriter")
set(SOURCE_FILES main.cpp)
add_executable(myproject ${SOURCE_FILES})
Any ideas how to fix it?
Consider adding include_directories(/path/to/include) and link_directories(/path/to/lib) before add_executable().
And then insert target_link_libraries(pngwriter) after add_executable().
/path/to shall be replaced with relevant values for your system.

How to statically link Qt5 with CMake in CLion?

I downloaded the sources of Qt and compiled it with -static flag into /usr/local/Qt5_static/. I created empty project and changed CMakeLists.txt like this:
cmake_minimum_required(VERSION 3.6)
project(Splines)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Wextra -pedantic -static -stdlib=libc++ -lc++abi -v")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
set(SOURCE_FILES main.cpp)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(Qt5_DIR "/usr/local/Qt5_static/")
find_package(Qt5Widgets)
add_executable(Splines ${SOURCE_FILES})
target_link_libraries(Splines Qt5::Widgets)
There is a linker error.
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/libQt5Widgets.so.5.7.0'
clang-3.8: error: linker command failed with exit code 1 (use -v to see invocation)
Looks like linker tried to link shared lib instead of static lib i compiled. I tried other variations of CMakeLists.txt but failed.
How to statically link Qt with cmake properly?
(Sorry for my bad english)
CMAKE_CXX_FLAGS are compiler flags, but target_link_libraries uses linker flags, which are CMAKE_EXE_LINKER_FLAGS. For static link you need to add -static to linker flags.

libcurl link with mingw and clion

I'm trying to build my project using curl, but I have this result :
undefined reference to `_imp__curl_easy_init'
This is my CMakeLists :
cmake_minimum_required(VERSION 2.8)
project(score)
set(SOURCE_FILES main.cpp)
add_executable(score ${SOURCE_FILES})
add_library(libcurl STATIC IMPORTED)
set_property(TARGET libcurl PROPERTY IMPORTED_LOCATION "c:/MinGW/lib")
SET(GCC_COVERAGE_LINK_FLAGS "-lcurl")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}" )
Moreover, I put my file libcurl.a, etc... in the correct directory "c:/MinGW/lib".
Could you help me ?
You probably need to compile the source files that call the curl functions with the CURL_STATICLIB macro defined.
Do you have access to the curl-config utility? It's there when you build curl from source. Run it with the --cflags option to get the compiler flags required and the --libs option to get linker requirements.
For example, in my mingw environment, the cflags reported are -DCURL_STATICLIB -I/mingw/local/include and the lib flags reported are -L/mingw/local/lib -lcurl -lssl -lcrypto -lgdi32 -lwldap32 -lz -lws2_32.

How to set compiler flags for groups of files in CMake?

In my CMake project I set up compiler flags like this:
if (MSVC)
# Build cpp files on all cores
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /W4")
# can't use the "secure" versions as they're Windows specific
add_definitions("/D\"_CRT_SECURE_NO_WARNINGS\"")
add_definitions("/D\"_SCL_SECURE_NO_WARNINGS\"")
add_definitions("/wd4290")
else()
# Enable C++11, you may need to use -std=c++0x if using an older gcc compiler
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wno-unused-parameter -fPIC -Wall -Weffc++ -pedantic")
endif()
endif()
To set the flags for MSVC, Clang and GCC. However in my source files var I have my source code and 3rd party things like gtest and gmock.
How can I set these flags such that they only apply to some subset of my source code?
E.g
# This should use the flags set above
SET(source
mycode/mysrc.cpp)
# This should not use the flags from above
SET(not_my_source
3rdparty/3rdparty.cpp)
# But both end up being part of the same executable
add_executable(Test ${source} ${not_my_source})
You may want to refer to here. You should be able to pass a list of files that you want to change the compiler flags for.