CMake How to add flags only for compilation, not linking - cmake

E.g., I want to pass -mllvm --color -mllvm --inline-threshold=1000 flags to clang in CMakeLists.txt.
However, if I use set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mllvm --color -mllvm --inline-threshold=1000"), the compilation will be fine, but I will get warnings during linking:
[2/2] Linking CXX executable test
clang: warning: argument unused during compilation: '-mllvm --color' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-mllvm --inline-threshold=1000' [-Wunused-command-line-argument]
What is the best way to append args only for compilation, not linking?
Note: add_compile_options() does not work. See Pass compound compiler options using cmake

Are you you are that's how you pass arugments to -mllvm?
-mllvm <value> Additional arguments to forward to LLVM's option processing
Seems like the --color should just be color. But I maybe wrong.
add_compile_options and target_compile_options are the two preferred methods to set compiler flags. With CMAKE_CXX_FLAGS being the old less ideal way. So I don't see any reason cmake would try to use those flags during linking, unless you are passing bad information.

Related

CMake and MinGW cannot disable "is deprecated" warnings also setting CMAKE_WARN_DEPRECATED

I am using CMake version 3.23 with MinGW compiler
This are the basic CMAKE settings:
CMAKE_MINIMUM_REQUIRED(VERSION 3.23)
## GENERAL INITIALIZATION
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
I use CMake -G "MinGW Makefiles" .. to build the project with MinGW generator and everything works. To compile I can use both CMake --build . or mingw32-make.exe: they both warks but gives the warnings that I am describing.
The problem is that I am using some external libraries (boost, turtle) that uses the template<class> class std::auto_ptr that is deprecated.
For this reason, when I compile my project, I get this warning:
C:/turtle/include/turtle/detail/action.hpp:220:22: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
mutable std::auto_ptr< Result > v_;
^~~~~~~~
And many other of this type as well.
As I mentioned, those warnings are not a problem for compilation, but I would like to disable them.
I have tried to set CMAKE_WARN_DEPRECATED:
SET(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "" FORCE)
but it does not work.
I have also tried
add_compile_options(-Wno-deprecated-declarations)
but it does not work as well.
EDIT:
As suggested in the comments by Alex Reinking CMAKE_WARN_DEPRECATED is not related to compilation warning but to CMAKE warning.
The only way I've found to eliminate the warnings was to redefine CMAKE_CXX_FLAGS:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
Hope it can be helpful

How to create CMakeLists for this make file [duplicate]

Work on Ubuntu 16
I used g++ main.cpp -lpq command for compiler my small project. Now I use Clion and wanna do same what I do with g++. But I can't add compiler flags in cmake file and get compile error.
cmake_minimum_required(VERSION 3.5.1)
project(day_g)
set(CMAKE_CXX_FLAGS "-lpq")
add_definitions(-lpq)
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(day_g ${SOURCE_FILES})
Also I run only cmake file and get CMAKE_CXX_FLAGS with -lpq flag.
CMAKE_CXX_FLAGS is -lpq
-- Configuring done
-- Generating done
How properly add compiler flags to cmake file?
Flag -l is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:
target_link_libraries(day_g pq)
-lq is not a compiler flag (CFLAGS) but a linker flag.
To pass a library in a CMake project you should use:
target_link_libraries(target_name libraries...)
Note that if you specify 'q' as library the project will link with libq.a or, if you are on windows q.dll.
... in your CMakeLists.txt the correct line to add is:
target_link_libraries(day_g pq)
Note also that when you add a CFLAG you should also "remember" the previous ones that may be added by libraries or by your platform, ie:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
To check the exact flags cmake is passing to compiler or linker you can always run, from the build directory, the following command:
make VERBOSE=1

Categories in static lib not regocnized at runtime

I am building an executable ("tool") on Linux. Using include $(GNUSTEP_MAKEFILES)/tool.make.
It's linked to a static lib that has also be build with GNUstep. The lib
contains Categories.
The executable builds fine but has errors at runtime not recognizing
methods defined in the static lib's Category:
Uncaught exception NSInvalidArgumentException, reason:
ClassNameOfClassTheCategoryExtends(instance) does not recognize
nameOfMethodInCategory
I am trying to fix that by passing -ObjC to the linker flags (also
tried -all_load) in the executable's GNUmakefile:
ADDITIONAL_LDFLAGS = -ObjC -all_load
But that seems to be ignored by clang. Here is the relevant output of
make install messages=yes debug=yes
clang: warning: argument unused during compilation: '-ObjC'
[-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-all_load'
[-Wunused-command-line-argument]
It looks like ADDITIONAL_LDFLAGS are used compiling, not linking.
Using this leads to the same result:
LDFLAGS := $(LDFLAGS) -ObjC
The excecutables GNUmakefileincludes the following:
include $(GNUSTEP_MAKEFILES)/common.make
# My make
include $(GNUSTEP_MAKEFILES)/tool.make
The resulting command line output is:
$ make install messages=yes debug=yes
This is gnustep-make 2.9.0. Type 'gmake print-gnustep-make-help' for help.
Running in gnustep-make version 2 strict mode.
Making all for tool NameOfExcecutable...
clang -ObjC -fuse-ld=/usr/bin/ld.gold -pthread -fexceptions -rdynamic -fobjc-runtime=gnustep-2.0 -fblocks -o obj/NameOfExcecutable \
./obj/NameOfExcecutable.obj/main.m.o ./obj/NameOfExcecutable.obj/MyClass.m.o ./obj/NameOfExcecutable.obj/StreamRunLoop.m.o ./obj/NameOfExcecutable.obj/Connector.m.o ./obj/NameOfExcecutable.obj/HTTPClient.m.o \
-L/home/user/GNUstep/Library/Libraries -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -ldispatch -l/path/to/libOwnLib1.a -l/path/to/libOwnLib2.a -l/path/to/libOwnHavingTheCategories.a -l/path/to/libOwnLib4.a -l/path/to/libOwnLib5.a -luuid -lz -lpthread -ldl -lpthread -lobjc -lm
clang: warning: argument unused during compilation: '-ObjC' [-Wunused-command-line-argument]
Question:
What am I doing wrong
or
How can I work around the issue?
After digging into the issue of the linker not knowing the -ObjC flag (which we are used to use in Xcode) it looks like:
only ld.ld64 is aware of this flag
ld.ld64 is a (too genericly named) "linker for macOS" (from LLDB.org)
thus is not available for Linux linkers
To workaround we first stopped using GNUstep makefiles to
disable all GNUstep magic understand what is going on and wrote our own makefiles.
The actual fix to force link/load all .o files was to explicitly pass --whole-archive to the linker:
-Wl,-whole-archive path/to/static/lib/containing/categories/libOwnLib1.a -Wl,-no-whole-archive

Compiler flags settings in CMAKE scripts

I am facing below issue while running my build:
C:/Test.cpp: In member function '........':
C:/Test.cpp:291:50: error: 'round_one' may be used uninitialized in this function [-Werror=maybe-uninitialized]
I tried to grep for string maybe-uninitialized in my whole source code but I could not find one. I was expecting some declaration like below:
set_source_files_properties(ROOT_DIR/Test.cpp PROPERTIES COMPILE_FLAGS "-Wno-maybe-uninitialized -Wno-misleading-indentation" )
or
SET(GCC_COVERAGE_COMPILE_FLAGS "-Wno-maybe-uninitialized")
add_definitions(${GCC_COVERAGE_COMPILE_FLAGS})
But I could not find any - please let me know how Compiler flags are set in CMAKE utility?
The warning -Wmaybe-uninitialized is one of those that are enabled
by -Wall.
-Wall is always specified by proficient programmers. Warnings will be converted
to errors by -Werror, so the flags -Wall -Werror will produce -Werror=maybe-uninitialized,
as per your diagnostic, if a potentially uninitialized variable is detected.
You will very likely find -Wall ... -Werror in the specified compiler flags in the relevant CMakeLists.txt
One way is setting the add compiler flag for the project:
cmake_minimum_required(VERSION 2.8)
# Project
project(008-compile-flags-01)
# Add compile flag
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHELLO_WORLD" CACHE STRING "Hello World Define." FORCE)
# Executable source files
set(executable_SOURCES src/main.cpp)
# Executable
add_executable(executable ${executable_SOURCES})
Other way is setting compiler flag for the target:
cmake_minimum_required(VERSION 3.2)
# Project
project(008-compile-flags-03)
# Executable source files
set(executable_SOURCES src/main.cpp)
# Executable
add_executable(executable ${executable_SOURCES})
# Add compile flag
target_compile_options(executable PRIVATE -DHELLO_WORLD)
Other way is using target_compile_features. I haven't used this before. Please see:
https://cmake.org/cmake/help/latest/command/target_compile_features.html
https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html

How to add a custom build type to cmake?

I want to add my build type to cmake to call cmake like this:
cmake -DCMAKE_BUILD_TYPE=mytype
I've written strings to my CMakeLists.txt:
set(CMAKE_CXX_FLAGS_DEBUG "-fPIC -o0 -g")
set(CMAKE_CXX_FLAGS_MYTYPE "-fPIC -o0 -g -m32)
set(CMAKE_CXX_FLAGS_RELEASE "-fPIC -o3)
But cmake uses compiler flags which were wrote in CMAKE_CXX_FLAGS_DEBUG.
What I should do to add my build type correctly?
I've fond soulution. set function must be before project() directive.