简体   繁体   中英

CMake OBJECT file flags not working

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM