How do I enable LinkTimeOptimization with Clion? [closed] - cmake

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'd like to have my program link time optimized. Where do I have to enter it to enable it?
If it matters (I hope it doesn't): I'm using MinGW-w64 5.0 on Windows.
edit: I really don't see why it wouldn't matter that I'm using CLion. I'm aware that - for now - it uses CMake as underlying build system.
But a) In the future CMake won't be the only build system that CLion will support (See here), so refering to CMake wouldn't solve the problem itself.
And b) adjusting the CMakeLists.txt would still require, that I configure each project individually. I asked for a way to configure the IDE, so it would do it for me.
I also don't this that it's an unsolvable problem per se. There could be a configuration or a PlugIn that does the trick. I failed to find it.. yet that doesn't mean that my question is unanswerable.

In general, this has nothing to do with CLion, but is a matter of CMake.
The problem, however, is that the appropriate flags differ among the compilers and linkers.
As you are using MinGW and, as far as I know that implies GCC, you can try the following as a rough starting point:
if(${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -flto")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} -flto")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} -flto")
endif()
However, I'd recommend using target properties (assuming you have a target MyTarget as an executable or shared library defined):
if(${CMAKE_CXX_COMPILER_ID} MATCHES GNU)
set_property(TARGET MyTarget
APPEND PROPERTY LINK_FLAGS -lto)
endif()

Related

Google Test. Microsoft Visual Studio 2022. How to create different files with test in one Google test project? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
Improve this question
I am using the latest version of MVS 2022. I have a solution with 2 projects. The second one is Google test project created from a template. I want to test the first one. In the first project I have a main.cpp, Sum.h, Sum.cpp. Sum.h has a simple declaration of function int Sum(int lhs, int rhs), Sum.cpp has it's defenition. In my Google test project i want to test function Sum(). I want to write different tests in different cpp files. In order to do it I need to include my Sum.h file either in pch.h file or in every test1.cpp, test2.cpp, .... file. Sum.h contains #pragma once.
Why when I include Sum.h file in pch.h i have an error: LNK2019, LNK1120 (smth like undefined ref to the element Sum(int, int))?
Why when I include Sum.h in both test1.cpp, test2.cpp file i receive a multiple defenition error.
How do I fix my problem?
Tried usin Microsoft Unit tests framework. The same problem.

Find out why cmake adds specific link flags

I have big project with cmake. It mostly works.
But recently some combination of compilation server vs test server broke. Investigation found that final compile/link command calls gcc (...) -licudata -licui18n -licuuc (...), this introduces dependency on shared library which is not present on test server.
How do I find out what in my project (my library, imported library, found library, whatever) adds those 3 flags to compile command?
I don't add them explicitly, so something is done automagically and I want to find it. compile_commands.json doesn't have them because linking flags don't belong in it. CMakeCache.txt has those flags in some obscure variable PC_LIBXML_STATIC_LIBRARIES:INTERNAL but removing them there doesn't affect compile/link command.
Note that this question is not about dealing with libicu specifically but about a method for investigation in general (though comments about eventual known problems with libicu would be appreciated too).
I found out that dependency graphs created by cmake can have more details that was configured for our project. Here are all options: https://cmake.org/cmake/help/latest/module/CMakeGraphVizOptions.html I expect GRAPHVIZ_EXTERNAL_LIBS, GRAPHVIZ_SHARED_LIBS are most important to set to true.
We enabled everything that was possible to enable, filtered out nothing and resulting graph was massive (to big for xdot - luckily .dot files are human readable), but showed that Boost::regex uses those 3 libraries.

Setting project wide compiler options [duplicate]

This question already has an answer here:
What is the modern method for setting general compile flags in CMake?
(1 answer)
Closed 1 year ago.
I want to use CMake to create modular embedded C++ software. I separated hal-drivers static library, some common-utils library and top target device depends on those two what is marked using target_link_libraries like this:
target_link_libraries(device
PRIVATE
hal-drivers
common-utils
)
It is easy to propagate compile definitions and option up in "dependency ladder" using commands like this:
target_compile_definitions(hal-drivers
INTERFACE
STM32F415xx
USE_HAL_DRIVER
)
This way any target utilising hal-drivers header files will preprocess those headers correctlym, and I found this CMake scripts feature (propagation of "settings") great, but it is not the point of this question.
The question is how should I propagate common compiler options like for example -fdata-sections or -Wall for every target in my project? I know I can
create dummy (no source and no header files, just compile options) interface target which will be consumed by every other target in project but this looks like a workaround...
specify mentioned compiler options for every target separatly, since I have only about 5 targets, but it will be very problematic to maintain.
In my commercial work project (50 targets) my boss ended up with an ugly compromise: setting common compile options in top CMakeLists.txt as cached variable and then applying this variable in all targets manually, but we dont like it at all.
Bear in mind: I do have solutions that work, I am interested in recomended solutions. Also I am using Professional CMake: A Practical Guide 9th Edition on daily basis (its a great book), but I failed to found answer on my question in this book.
I found an answer.
I guess my whining about lack of elegant solution is due to my attachment to syntactic sugar like target_compile_options, but the thing is CMake evolved in hardship and not every CMake feature is pretty, but it works.
There is an answer: https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html
The flags in this variable will be passed to the compiler before those in the per-configuration CMAKE_<LANG>_FLAGS_<CONFIG> variant, and before flags added by the add_compile_options() or target_compile_options() commands.
So I have to append my custom options to this special CMake variable like this:
project(Device C CXX ASM)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fdata-sections")
This way it will flood all targets with fdata-sections.
Leaving this thread as interesting note.

What is the point of ALIAS in cmake? [duplicate]

This question already has answers here:
Using alias targets in CMake
(1 answer)
cmake usefulness of aliases
(2 answers)
Closed 4 years ago.
In a lot of tutorials and cmake examples I see, it is suggested that you provide aliases with ::s in the name:
add_library(foo a.cxx b.cxx c.xx)
add_library(N::foo ALIAS foo)
What is the point of doing such a thing? Is there a case where using N::foo subsequently solves a problem that using foo directly might have? Why is this considered good practice?
Citing the documentation regarding alias targets:
A primary use-case for ALIAS targets is for example or unit test
executables accompanying a library, which may be part of the same
buildsystem or built separately based on user configuration.
add_library(lib1 lib1.cpp)
install(TARGETS lib1 EXPORT lib1Export ${dest_args})
install(EXPORT lib1Export NAMESPACE Upstream:: ${other_args})
add_library(Upstream::lib1 ALIAS lib1)
With other words, as alias with different computer systems, it makes the names less technical and more human-readable / user-friendly.

Multi-language command-line source code formatter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there a command-line Unix tool that will format/indent/prettify source code in different languages? I'm especially interested in Java, JavaScript, PHP, and XML, but ideally it would handle others.
(I'm not looking for something to generate syntax-highlighting markup; I already know of a few tools that do that.)
Artistic Style.
http://astyle.sourceforge.net/
If you have set your auto-formatting options as project-specific settings in Eclipse, you can do something like:
/opt/local/eclipse/eclipse -nosplash
-application org.eclipse.jdt.core.JavaCodeFormatter
-verbose
-config .settings/org.eclipse.jdt.core.prefs
src/ tests/ doc/examples/
This means that you practically install and configure Eclipse for this purpose if only for using it's autoformatting features, regardless of what editor you use normally. :)
Source: http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line
Additional Notes
On Mac OS X:
/Applications/eclipse/java-oxygen/Eclipse.app/Contents/MacOS/eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -verbose -config ~/my-eclipse-workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs MyClass.java
I've always found Vim's code formatter a great option. It is aware of many languages and can be reasonably customized.
You can pipe the relevant commands into vim like this:
vim MyClass.java <<< gg=G:wq
Explanation:
gg=G formats the file
:wq saves the file and returns to the command prompt
Check out indent and enscript.
Vim generally has automatic syntax highlighting and is available on most Unix-based systems when you install. For formatting and indentation in Vim I use the :set autoindent and :set tabstop=4 automatically when I start it. autoindent keeps the current indentation you are at when you start a new line, and tabstop sets how much your code is indented when you press tab (only for indentation, for tab in general use shiftwidth). To have these options configured whenever you start Vim put them in a ~/.vimrc file.
For XML and HTML I have used htb.
If you are an Eclipse user then JTidy is another option.
For Java there is Jalopy.
So, I bring to your attention Style Revisor, source code formatter with GUI and command-line interface. It will be support different languages, include JavaScript and PHP. If you're interested in command-line usage - you can define your own formatting style as addon. Of course, you can also use many predefined styles. Example:
./Style\ Revisor --lang=PHP --style=GNU --path=~/to-your-project-root-dir
Currently, Style Revisor supports two languages: C and Objective-C. Welcome: http://style-revisor.com/
Sincerely.