How to build with OpenCM3 + FreeRTOS + CMake to ARM CM4? - c++

I am trying to program an ARM STM32F407 on an STM32F4Discovery board. First, I built a project with OpenCM3 + CMakeLists.txt. It worked! The LED flashed. ;-) Now, I'm trying to make the same project with OpenCM3 + FreeRTOS + CMakeLists.txt. Is not working! :-( It is not able to link xTaskCreate and vTaskDelay.
Please can anyone see where I'm going wrong?
I got my FreeRTOSConfig.h from here.
Below I show the error messages and my CMakeLists.txt file.
[100%] Linking C executable TesteRTOS.elf
/usr/local/Cellar/arm-none-eabi-gcc/10.3-2021.07/gcc/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: CMakeFiles/TesteRTOS.dir/src/Teste.c.o: in function `GreenLEDTask':
Teste.c:(.text.GreenLEDTask+0x16): undefined reference to `vTaskDelay'
/usr/local/Cellar/arm-none-eabi-gcc/10.3-2021.07/gcc/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: CMakeFiles/TesteRTOS.dir/src/Teste.c.o: in function `main':
Teste.c:(.text.main+0x20): undefined reference to `xTaskCreate'
/usr/local/Cellar/arm-none-eabi-gcc/10.3-2021.07/gcc/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: Teste.c:(.text.main+0x24): undefined reference to `vTaskStartScheduler'
collect2: error: ld returned 1 exit status
make[2]: *** [TesteRTOS.elf] Error 1
make[1]: *** [CMakeFiles/TesteRTOS.dir/all] Error 2
make: *** [all] Error 2
cmake_minimum_required(VERSION 3.13)
set(HAVE_FLAG_SEARCH_PATHS_FIRST 0)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(TesteRTOS C CXX ASM)
set(PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(MCU_LINKER_SCRIPT ${PROJECT_DIR}/linkerscript.ld)
set(MCU_ROM_ADDRESS 0x08000000)
set(CPU_PARAMETERS
-mcpu=cortex-m4
-mthumb
-mfpu=fpv4-sp-d16
-mfloat-abi=hard
)
set(OPENCM3_COMPILER_FLAGS -DSTM32F4)
set(OPENCM3_MCU_LIB opencm3_stm32f4)
set(RTOS_PATH ~/Documents/STM32/FreeRTOS-Kernel)
set(RTOS_PORTABLE ARM_CM4F)
enable_language(C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
# Specify the cross compiler
SET(CMAKE_C_COMPILER /usr/local/bin/arm-none-eabi-gcc)
SET(CMAKE_CXX_COMPILER /usr/local/bin/arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER /usr/local/bin/arm-none-eabi-gcc)
set(CMAKE_AR /usr/local/bin/arm-none-eabi-ar)
SET(CMAKE_OBJCOPY /usr/local/bin/arm-none-eabi-objcopy)
SET(CMAKE_SIZE /usr/local/bin/arm-none-eabi-size)
file(GLOB_RECURSE PROJECT_SOURCES FOLLOW_SYMLINKS
${PROJECT_DIR}/*.cpp
${PROJECT_DIR}/*.c
)
add_executable(${CMAKE_PROJECT_NAME}
${PROJECT_SOURCES}
)
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
SUFFIX ".elf"
)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
$ENV{OPENCM3_PATH}/include
${PROJECT_DIR}
${RTOS_PATH}/include
)
FILE(GLOB FreeRTOS_src ${RTOS_PATH}/*.c)
add_library(FreeRTOS STATIC
${FreeRTOS_src}
${RTOS_PATH}/portable/GCC/${RTOS_PORTABLE}/port.c
${RTOS_PATH}/portable/MemMang/heap_4.c
)
target_include_directories(FreeRTOS PUBLIC
${RTOS_PATH}/include
${RTOS_PATH}/portable/GCC/${RTOS_PORTABLE}/
${PROJECT_DIR}
)
target_compile_options(FreeRTOS PRIVATE
${CPU_PARAMETERS}
${OPENCM3_COMPILER_FLAGS}
-Wall -Wextra -Wundef -Wshadow -Wredundant-decls
-fno-common -ffunction-sections -fdata-sections -MD
$<$<CONFIG:Debug>:-Og -g3 -ggdb>
$<$<CONFIG:Release>:-Og -g0>
)
target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE
${CPU_PARAMETERS}
${OPENCM3_COMPILER_FLAGS}
-Wall -Wextra -Wundef -Wshadow -Wredundant-decls
-fno-common -ffunction-sections -fdata-sections -MD
$<$<CONFIG:Debug>:-Og -g3 -ggdb>
$<$<CONFIG:Release>:-Og -g0>
)
target_link_options(${CMAKE_PROJECT_NAME} PRIVATE
--static -nostartfiles
--specs=nosys.specs
-T${MCU_LINKER_SCRIPT} -L$ENV{OPENCM3_PATH}/lib
${CPU_PARAMETERS}
-ggdb3 -Wl,-Map=${CMAKE_PROJECT_NAME}.map
-Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
-Wl,--cref -Wl,--gc-sections
)
target_link_libraries(${CMAKE_PROJECT_NAME} ${OPENCM3_MCU_LIB} FreeRTOS)
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${CMAKE_PROJECT_NAME}>)
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
${CMAKE_PROJECT_NAME}.hex
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}>
${CMAKE_PROJECT_NAME}.bin
COMMENT "Creating ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex."
)
add_custom_target(FLASH
st-flash --reset write ${PROJECT_NAME}.bin ${MCU_ROM_ADDRESS}
DEPENDS ${CMAKE_PROJECT_NAME}
COMMENT "Flashing ${PROJECT_NAME}"
)

Related

How to print out debug info to terminal when running c++ with CMake?

I am reading a C++ project and want to print out the #ifdef _DEBUG message when running the program at a Linux terminal. For example:
#ifdef _DEBUG
cout << s1 << endl;
#endif
Currently, it doesn't print out the debug info above, but only prints out logger info as below:
logger_(MY_MODULE_LOG_ERROR, "config is null ");
The project is made through cmake. It has a top level CMakeLists.txt file, in addition to each CZMakeLists.txt under src/ and its subdirectories. The content of the top-level CMakelists.txt is below:
cmake_minimum_required (VERSION 3.12)
project (TAGS_NEW )
set(CMAKE_VERBOSE_MAKEFILE true)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -w")
# gdb debug : -g
add_compile_options(
-Wall
-Wextra
-Wstrict-aliasing
-Wno-unused-parameter
-Wno-missing-field-initializers
-Wchar-subscripts
-Wpointer-arith
-Wformat
-Wformat-security
-Werror=format-security
-fstack-protector-all
-fPIE
-fpie
-fPIC
-fpic
-pipe
-fdata-sections
-ffunction-sections
)
option(DEBUG_OUTPUT "option for debug out" OFF)
if (DEBUG_OUTPUT)
add_definitions(-D_DEBUG)
endif()
# option(DEBUG_GDB "option for gdb debug" OFF)
# if (DEBUG_GDB)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -ggdb")
# endif()
# set(CMAKE_BUILD_TYPE "Debug")
option(CMAKE_BUILD_TYPE "option for gdb debug" DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
option(COMPILE_DLL "option for generate dynamic library" OFF)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${TAGS_NEW_SOURCE_DIR}/output)
add_definitions(-D_Python_CALL -D_ChaiScriptON)
include_directories(${TAGS_NEW_SOURCE_DIR}/third-party/include ${TAGS_MINING_NEW_SOURCE_DIR}/include )
# $(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")
find_package( PythonLibs 2.7 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )
link_directories(${TAGS_NEW_SOURCE_DIR}/third-party/lib)
add_subdirectory (src)
execute_process(COMMAND sh genGlobal.sh ${TAGS_NEW_SOURCE_DIR} WORKING_DIRECTORY ${TAGS_NEW_SOURCE_DIR})
I tried to change the OFF to ON in the line below, but it doesn't help:
option(DEBUG_OUTPUT "option for debug out" OFF)
I am new to CMake. How to print out all the debug info?
Options are meant to be provided from outside not to be modified in the CMake file. In fact you can't change an option with the option command once it is set in the cache (after the first CMake run). So run your cmake like this: cmake -DDEBUG_OUTPUT=ON .. and you will get your macro defined.

Error in importing opencv c++ file using cmake

opencv has been setup in my project but the imported c++ files are giving error
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
Build command failed.
Error while executing process C:\src\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C C:\Users\azad prajapat\AndroidStudioProjects\sunscape\sunscape\android\app.cxx\cmake\debug\armeabi-v7a native-panorama}
ninja: Entering directory `C:\Users\azad prajapat\AndroidStudioProjects\sunscape\sunscape\android\app.cxx\cmake\debug\armeabi-v7a'
[1/2] Building CXX object CMakeFiles/native-panorama.dir/src/main/jni/com_example_sunscape_NativePanorama.cpp.o
FAILED: CMakeFiles/native-panorama.dir/src/main/jni/com_example_sunscape_NativePanorama.cpp.o
C:\src\Android\Sdk\ndk\22.0.7026061\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi24 --gcc-toolchain=C:/src/Android/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/src/Android/Sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Dnative_panorama_EXPORTS -I../../../../src/main/jniIncludes -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -std=gnu++11 -Wno-error=format-security -frtti -fexceptions -stdlib=libstdc++ -O0 -fPIC -MD -MT CMakeFiles/native-panorama.dir/src/main/jni/com_example_sunscape_NativePanorama.cpp.o -MF CMakeFiles\native-panorama.dir\src\main\jni\com_example_sunscape_NativePanorama.cpp.o.d -o CMakeFiles/native-panorama.dir/src/main/jni/com_example_sunscape_NativePanorama.cpp.o -c ../../../../src/main/jni/com_example_sunscape_NativePanorama.cpp
In file included from ../../../../src/main/jni/com_example_sunscape_NativePanorama.cpp:3:
In file included from ../../../../src/main/jniIncludes\opencv2/opencv.hpp:52:
In file included from ../../../../src/main/jniIncludes\opencv2/core.hpp:52:
**../../../../src/main/jniIncludes\opencv2/core/cvdef.h:183:10: fatal error: 'limits' file not found
#include
1 error generated.**
ninja: build stopped: subcommand failed.
my cmake file
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
SET(CMAKE_EXE_LINKER_FLAGS "-lopencv_stitching")
include_directories(${CMAKE_SOURCE_DIR}/src/main/jniIncludes)
add_library(native-panorama
SHARED
src/main/jni/com_example_sunscape_NativePanorama.cpp)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/libs/${ANDROID_ABI}/libopencv_java4.so)
find_library(log-lib
log)
target_link_libraries(native-panorama ${log-lib} lib_opencv)
gradle file
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang++","-DANDROID_ARM_NEON=TRUE" ,"-DANDROID_STL_FORCE_FEATURES=OFF"
cppFlags "-std=gnu++11", "-Wno-error=format-security","-frtti -fexceptions"
}
}
ndk {
abiFilters 'x86', 'x86_64', 'armeabi-v7a',
'arm64-v8a'
}
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
Don't do that.

Why two targets linking differently using cmake?

I have this target that compiles ok.
#
file(GLOB SOURCE_FILES
8021QBG/*.h
8021QBG/*.cpp
)
add_library(8021qbg SHARED
${SOURCE_FILES}
)
set_target_properties(8021qbg PROPERTIES LINKER_LANGUAGE CXX)
set(CMAKE_CXX_FLAGS_CUSTOM " ")
set_target_properties(8021qbg PROPERTIES COMPILE_FLAGS " ${CMAKE_CXX_FLAGS_CUSTOM} ${CMAKE_CXX_FLAGS_COMMON}")
add_dependencies(8021qbg core )
target_link_libraries(8021qbg -L${PROJECT_BINARY_DIR})
target_link_libraries(8021qbg -L/home/user/protocol_so )
target_link_libraries(8021qbg -L${PROJECT_SOURCE_DIR}/lib64 )
target_link_libraries(8021qbg -L${PROJECT_SOURCE_DIR})
target_link_libraries(8021qbg protocol_common thread vip core m xml2)
target_include_directories(8021qbg PUBLIC
8021QBG
nte-encap
)
and this that fails.
#
file(GLOB SOURCE_FILES
Agent/*.h
Agent/*.cpp
)
add_library(agent SHARED
${SOURCE_FILES}
)
set_target_properties(agent PROPERTIES LINKER_LANGUAGE CXX)
set(CMAKE_CXX_FLAGS_CUSTOM " ")
set_target_properties(agent PROPERTIES COMPILE_FLAGS " ${CMAKE_CXX_FLAGS_CUSTOM} ${CMAKE_CXX_FLAGS_COMMON}")
add_dependencies(agent core)
target_link_libraries(agent -L${PROJECT_BINARY_DIR})
target_link_libraries(agent -L/home/user/protocol_so )
target_link_libraries(agent -L${PROJECT_SOURCE_DIR}/lib64 )
target_link_libraries(agent -L${PROJECT_SOURCE_DIR})
target_link_libraries(agent protocol_common thread vip core xml2 pthread)
target_include_directories(agent PUBLIC
Agent
nte-encap
)
Also these targets have the same flags:
set (CMAKE_CXX_FLAGS_COMMON "-g -fpermissive -std=gnu89 -Wall -O0 -m64 -fPIC -DLINUX -DCPU_64 -DXSTREAM ")
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--unresolved-symbols=ignore-in-shared-libs -shared -Wl,-rpath,/home/user/protocol_so:/home/user/cmake_libs/lib64:libwifi")
if(NOT CMAKE_CXX_CREATE_SHARED_LIBRARY)
set(CMAKE_CXX_CREATE_SHARED_LIBRARY
"<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
endif()
This is linker stage
/usr/bin/g++ -fPIC -g -Wl,--unresolved-symbols=ignore-in-shared-libs -shared -Wl,-rpath,/home/user/protocol_so:/home/user/cmake_libs/lib64:libwifi -shared -Wl,-soname,libagent.so -o libagent.so CMakeFiles/agent.dir/Agent/agent_common.cpp.o CMakeFiles/agent.dir/Agent/agent_main.cpp.o CMakeFiles/agent.dir/Agent/agent_client.cpp.o CMakeFiles/agent.dir/Agent/agent_cmd.cpp.o CMakeFiles/agent.dir/Agent/agent_go.cpp.o -L/home/user/cmake_libs/cmake-build-debug-rurem -L/home/user/protocol_so -L/home/user/cmake_libs/lib64 -L/home/user/cmake_libs -lprotocol_common -lthread -lvip libcore.so -lxml2 -lpthread -L/home/user/cmake_libs/lib64 -lprotocol_common -lthread -lvip -lcrypto -lglib-2.0 -lm -lxml2 -lpthread -Wl,-rpath,/home/user/cmake_libs/cmake-build-debug-rurem
This is error codes
/home/user/cmake_libs/Agent/agent_main.cpp:47: undefined reference to `agent_client_register'
/home/user/cmake_libs/Agent/agent_main.cpp:48: undefined reference to `agent_client_unregister'
/home/user/cmake_libs/Agent/agent_main.cpp:49: undefined reference to `agent_client_register_task'
/home/user/cmake_libs/Agent/agent_main.cpp:50: undefined reference to `agent_client_unregister_task'
Cmake compiles each *.cpp file to *.o object and than linker combine them in one *.so lib -> in the linker stage I got this error.
Sources located in "Agent" folder and I add it to the target 'agent'.
UDP>
simple makefile and GCC build this library.
makefile part:
libagent.so: ${wildcard Agent/*.[c,h]} libcore.so
$(CC) $(CFLAGS) -fPIC -shared -o $# $(filter %.c,$^) $(IPATH) $(RPATH) $(TE_VIP) -lcore -lxml2 -lpthread
gcc command
gcc -g -Wall -O0 -m64 -Wl,--unresolved-symbols=ignore-in-shared-libs -fPIC -shared -o libagent.so -Inte-include -Ixst_inc -Ixst_tls -L./lib64 -L./cmake-build-debug-rurem -Wl,-rpath,/home/user/protocol_so:/home/user/lib64:libwifi -DLINUX -DCPU_64 -DXSTREAM -lprotocol_common -lthread -lvip -lcore -lxml2 -lpthread
UDP#2.
on arm machine with gcc v9.1.0 it builds successfully via makefile.
on x86 machine with gcc v4.3.4 I have such undefined referencies.
this is compiling command
g++ -g -Wall -O0 -m64 -Wl,--unresolved-symbols=ignore-in-shared-libs -fPIC -shared -o 0libagent.so -Inte-include -Iinc -Itls -L/home/user/cmake_libs/lib64 -L/home/user/cmake_libs -Wl,-rpath,/home/user/protocol_so:/home/user/cmake_libs/lib64:libwifi -DLINUX -DCPU_64 -DXSTREAM -lprotocol_common -lthread -lvip -lcore -lxml2 -lpthread Agent/agent_main.cpp Agent/agent_cmd.cpp Agent/agent_client.cpp Agent/agent_go.cpp Agent/agent_common.cpp
This is piece of agent_main.cpp that causes errors:
This is function that defined in agent_client.cpp that causes undefined reference
UPD#3
nm says that definitions are in objects.
> nm CMakeFiles/agent.dir/Agent/agent_client.cpp.o
...
00000000000007e2 T _Z21agent_client_registeriP20_protocol_callback_t
0000000000000000 T _Z21agent_client_run_taskiPv
0000000000000033 T _Z22agent_client_do_actionPviS_S_
...
x86 machine - SUSE 11 sp3 x64
Why it fails?
How to fix it?
The main point was not in cmake, and even not in make commands. I think this is about gcc versions support. On x86 I have gcc v4.3.4.
As #AlexCohn said in the comments
I would try to declare agent_client_register() and the rest of them as
extern "C" in agent_client.h and make sure this header is included in
both agent_client.cpp and agent_main.cpp

cmake: adding C++ standard when not required

When building a C++ executable under Linux using cmake 3.7, I see a -std=gnu++11 flag being added to compile flags. The problem is, I'm already manually adding a -std=c++1z flag, and this new one overwrites mine. This happens only for executables, but I cannot find this being mentioned in the docs. The CMAKE_CXX_STANDARD is empty, and setting the CXX_STANDARD property on the target has no effect. Is there a way to remove this flag?
This seems to be not only limited to executables.
Here's my (simplified) cmake:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z")
find_boost(serialization system)
find_package(Qt5Widgets REQUIRED)
link_directories(${Boost_LIBRARY_DIRS})
include_directories(
${Boost_INCLUDE_DIRS}
${ZMQ_INCLUDE_DIR}
${CPPZMQ_INCLUDE_DIR}
)
if(WIN32)
add_definitions(-DNOMINMAX)
endif()
add_executable(
${PROFILER_CLIENT_NAME}
main.cpp
MainWindow.cpp
MainWindow.h
ProfilerWidget.cpp
ProfilerWidget.h
TimelineWidget.cpp
TimelineWidget.h
ZmqReceiver.cpp
ZmqReceiver.h
)
add_dependencies(${PROFILER_CLIENT_NAME} boost zeromq)
target_link_libraries(
${PROFILER_CLIENT_NAME}
PRIVATE ${PROFILER_NAME}
PRIVATE ${Boost_LIBRARIES}
PRIVATE Qt5::Widgets
)
As #florian suspected, it's Qt5 that's polluting your compile commands. Using a similar CMakeLists.txt:
cmake_minimum_required(VERSION 3.7.2 FATAL_ERROR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z")
set(BOOST_ROOT "/usr/local/opt/boost#1.55")
execute_process(COMMAND brew --prefix qt5
COMMAND tr -d \\n
OUTPUT_VARIABLE QT5_BREW_PATH)
find_package(Boost COMPONENTS serialization system)
find_package(Qt5 COMPONENTS Widgets HINTS ${QT5_BREW_PATH})
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo main.cpp)
target_link_libraries(foo
PRIVATE ${Boost_LIBRARIES}
PRIVATE Qt5::Widgets
)
I configured and built a dummy executable. You can plainly see the -std=c++1z and the -std=gnu++11 on the compile line:
❯ make VERBOSE=1
/usr/local/Cellar/cmake/3.7.2/bin/cmake -H/Users/nega/foo -B/Users/nega/foo --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_progress_start /Users/nega/foo/CMakeFiles /Users/nega/foo/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/depend
cd /Users/nega/foo && /usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/nega/foo /Users/nega/foo /Users/nega/foo /Users/nega/foo /Users/nega/foo/CMakeFiles/foo.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/build
[ 50%] Building CXX object CMakeFiles/foo.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -I/usr/local/opt/boost#1.55/include -iframework /usr/local/opt/qt5/lib -isystem /usr/local/opt/qt5/lib/QtWidgets.framework/Headers -isystem /usr/local/opt/qt5/lib/QtGui.framework/Headers -isystem /System/Library/Frameworks/OpenGL.framework/Headers -isystem /usr/local/opt/qt5/lib/QtCore.framework/Headers -isystem /usr/local/opt/qt5/./mkspecs/macx-clang -std=c++1z -fPIC -std=gnu++11 -o CMakeFiles/foo.dir/main.cpp.o -c /Users/nega/foo/main.cpp
[100%] Linking CXX executable foo
/usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_link_script CMakeFiles/foo.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=c++1z -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/foo.dir/main.cpp.o -o foo -L/usr/local/opt/boost#1.55/lib -Wl,-rpath,/usr/local/opt/boost#1.55/lib /usr/local/opt/boost#1.55/lib/libboost_serialization-mt.dylib /usr/local/opt/boost#1.55/lib/libboost_system-mt.dylib /usr/local/opt/qt5/lib/QtWidgets.framework/QtWidgets /usr/local/opt/qt5/lib/QtGui.framework/QtGui /usr/local/opt/qt5/lib/QtCore.framework/QtCore
[100%] Built target foo
/usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_progress_start /Users/nega/foo/CMakeFiles 0
If you comment out the Qt5 usage in our CMakeLists.txt and configure and build again, you'll see the -std=gnu++11 disappear (along with the -fPIC which Qt is also adding).
CMakeLists.txt:
cmake_minimum_required(VERSION 3.7.2 FATAL_ERROR)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z")
set(BOOST_ROOT "/usr/local/opt/boost#1.55")
execute_process(COMMAND brew --prefix qt5
COMMAND tr -d \\n
OUTPUT_VARIABLE QT5_BREW_PATH)
find_package(Boost COMPONENTS serialization system)
#find_package(Qt5 COMPONENTS Widgets HINTS ${QT5_BREW_PATH})
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo main.cpp)
target_link_libraries(foo
PRIVATE ${Boost_LIBRARIES}
# PRIVATE Qt5::Widgets
)
make output (abridged):
[...]
[ 50%] Building CXX object CMakeFiles/foo.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/usr/local/opt/boost#1.55/include -std=c++1z -o CMakeFiles/foo.dir/main.cpp.o -c /Users/nega/foo/main.cpp
[100%] Linking CXX executable foo
/usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_link_script CMakeFiles/foo.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=c++1z -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/foo.dir/main.cpp.o -o foo -L/usr/local/opt/boost#1.55/lib -Wl,-rpath,/usr/local/opt/boost#1.55/lib /usr/local/opt/boost#1.55/lib/libboost_serialization-mt.dylib /usr/local/opt/boost#1.55/lib/libboost_system-mt.dylib
[100%] Built target foo
[...]
Unfortunately, after some brief digging I couldn't see where Qt was setting -std=gnu++11 in its *Config.cmake files. It must be reaching into CMake more than just a few grep's could find. Maybe reading through cmake --trace will provide some insight.
Curiously though, what ever it's doing respects CXX_STANDARD. If we tweak our original CMakeLists.txt and configure and build again:
CMakeLists.txt (abridged):
cmake_minimum_required(VERSION 3.7.2 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z")
set(BOOST_ROOT "/usr/local/opt/boost#1.55")
execute_process(COMMAND brew --prefix qt5
[...]
make output (abridged):
[...]
[ 50%] Building CXX object CMakeFiles/foo.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -I/usr/local/opt/boost#1.55/include -iframework /usr/local/opt/qt5/lib -isystem /usr/local/opt/qt5/lib/QtWidgets.framework/Headers -isystem /usr/local/opt/qt5/lib/QtGui.framework/Headers -isystem /System/Library/Frameworks/OpenGL.framework/Headers -isystem /usr/local/opt/qt5/lib/QtCore.framework/Headers -isystem /usr/local/opt/qt5/./mkspecs/macx-clang -std=c++1z -fPIC -std=gnu++14 -o CMakeFiles/foo.dir/main.cpp.o -c /Users/nega/foo/main.cpp
[100%] Linking CXX executable foo
/usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_link_script CMakeFiles/foo.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=c++1z -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/foo.dir/main.cpp.o -o foo -L/usr/local/opt/boost#1.55/lib -Wl,-rpath,/usr/local/opt/boost#1.55/lib /usr/local/opt/boost#1.55/lib/libboost_serialization-mt.dylib /usr/local/opt/boost#1.55/lib/libboost_system-mt.dylib /usr/local/opt/qt5/lib/QtWidgets.framework/QtWidgets /usr/local/opt/qt5/lib/QtGui.framework/QtGui /usr/local/opt/qt5/lib/QtCore.framework/QtCore
[100%] Built target foo
[...]
You can see that the (Qt added) -fPIC -std=gnu++11 is now -fPIC -std=gnu++14. Unfortunately this won't help you until CMake 3.8.0 is released and its CXX_STANDARD/CMAKE_CXX_STANDARD will understand "C++17".

Replace -fPIC with -fpic

When using Qt CMake automatically adds the -fPIC flag to compile options. I want to use -fpic, so I went through all Cmake variables and replaced -fPIC with -fpic.
cmake_minimum_required(VERSION 3.5)
project(sss)
find_package(Qt5 REQUIRED COMPONENTS Core Sql)
get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
if (NOT "${${_variableName}}" STREQUAL "")
string(REPLACE "-fPIC" "-fpic" ${_variableName} ${${_variableName}})
string(REPLACE "-fPIE" "-fpie" ${_variableName} ${${_variableName}})
endif()
#message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
set(CMAKE_CXX_FLAGS "-fpie")
set(CMAKE_EXE_LINKER_FLAGS "-fpie -pie")
add_executable(sss main.cpp)
target_link_libraries(sss Qt5::Core Qt5::Sql)
main.cpp contains
#include <QSqlDatabase>
int main(){
QSqlDatabase::addDatabase("QPSQL");
}
Unfortunately CMake still adds the -fPIC flag, althoguh the listed variables does not contain it:
Building CXX object CMakeFiles/sss.dir/main.cpp.o
/usr/bin/c++ -DQT_CORE_LIB -DQT_NO_DEBUG -DQT_SQL_LIB -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -isystem /usr/include/x86_64-linux-gnu/qt5/QtSql -fPIC -o CMakeFiles/sss.dir/main.cpp.o -c src/main.cpp
How can I replace -fPIC with -fpic?
Turning my comment into an answer
Your code overwrites CMake global variables like CMAKE_CXX_COMPILE_OPTIONS_PIC or CMAKE_CXX_COMPILE_OPTIONS_PIE.
But Qt brings its own -fPIC option through target properties. The Qt5::Core target does have INTERFACE_COMPILE_OPTIONS set to -fPIC (see e.g. here).
Try overwriting the target properties by adding
set_property(TARGET Qt5::Core PROPERTY INTERFACE_COMPILE_OPTIONS "-fpic")
after your find_package(Qt5 ...) call.