Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning on linker flags with CMake

When generating VS2010 targets with CMake, I would like the /LTCG flag turned on (only for release + releasewithdebinfo if possible, but its okay if its on for debug builds). How do I modify the linker flags? add_definitions() doesn't work because that only modifies compiler flags. And yes, I have wrapped it in if(MSVC).

How do I modify the linker flags?

like image 523
Clark Gaebel Avatar asked Aug 23 '10 01:08

Clark Gaebel


People also ask

How do I attach a linker flag?

Right-click the target in the Xcode Groups and Files list and select Get Info from the contextual menu. In the Build tab, type linker into the search field and then locate the Other Linker Flags item. Double-click the Other Linker Flags item and add -lgmp .

How do I add GCC flags?

Open your project and then go Project > Build Options > Compiler Flags . You can tick boxes in the "Compiler Flags" tab, and you can write other options in the "Other Options" tab. Do one or the other, e.g. don't tick the "-std=c++98" box and also put "-std=c++11" in the Other Options.

Does CMake use Cflags?

This is a CMake Environment Variable. Its initial value is taken from the calling process environment. Default compilation flags to be used when compiling C files.


3 Answers

You can modify the linker flags in MSC using #pragma comment(linker, ...)

However, if you'd like to do it in the build process with cmake, here are the names you need to know:

  • CMAKE_EXE_LINKER_FLAGS
  • CMAKE_SHARED_LINKER_FLAGS
  • CMAKE_MODULE_LINKER_FLAGS

(Thanks to Cmake.org).

like image 66
DMags Avatar answered Sep 25 '22 00:09

DMags


and STATIC_LIBRARY_FLAGS http://www.cmake.org/cmake/help/v2.8.8/cmake.html#prop_tgt:STATIC_LIBRARY_FLAGS

for static libraries

like image 40
elegant dice Avatar answered Sep 26 '22 00:09

elegant dice


For adding linker flags - the following 4 CMake variables:

CMAKE_EXE_LINKER_FLAGS
CMAKE_MODULE_LINKER_FLAGS
CMAKE_SHARED_LINKER_FLAGS
CMAKE_STATIC_LINKER_FLAGS

can be easily manipulated for different configs (debug, release...) with the ucm_add_linker_flags macro of ucm

linker flags can also be managed on a per-target basis - by using target_link_libraries and passing flags with a - in front of them (but not with a -l - that would be treated as a link library and not a link flag).

like image 26
onqtam Avatar answered Sep 25 '22 00:09

onqtam