-2

I am working on a C project which I converted from MATLAB using MATLAB Coder. I made a lot of changes to optimize the C code since it was pretty slow after the translation. In order to further increase the execution speed of the program, I tried to add compiler optimization flag in the CMakeLists, which significantly reduced the execution time, but at the same time making the program crash which was most likely to happen as explained here https://stackoverflow.com/a/7857672/9302627. To make sure what was going on, I ran the optimized code in Clion which I am using for my project, and found out that a lot of variable got their values changed and hence the logic of the program was being affected.

I know that my function "ZCNET_detec_pkt.c" is the one that takes the most amount of time because it contains the main algorithm of my project and I am trying to optimize it. At this moment, I plan to optimize one source file at a time so that it will be easy to debug.

I went through some blogs to learn "How to optimize a single source file in Cmake" but so for nothing is working. I tried to use #pragma optimize as mentioned in this blog, https://stackoverflow.com/a/44978687/9302627 but that didn't work. I tried to add the optimization flag using set_source_files_properties but that didn't work. It is possible that I might be doing it wrong but I am not experienced with Cmake so I am still learning.

The CMakeLists.txt that I am using was generated by Clion looks something like this at this moment.

cmake_minimum_required(VERSION 3.10)
project(zc_dec C)

set(CMAKE_C_STANDARD 11)

add_executable(zc_dec
        abs.c
        abs.h
        bin2dec.c
        bin2dec.h
        circshift.c
        circshift.h
        .
        .
        .
        . )

set_source_files_properties(ZCNET_detec_pkt.c CMAKE_C_FLAGS "-O3")
target_sources(zc_dec PRIVATE
        ZCNET_detec_pkt.c
        ZCNET_detec_pkt.h)

target_link_libraries(zc_dec m -lfftw3 -lm)

I am trying figure out how to can add compiler optimization flag for one specific source file at a time. Any Help is appreciated. Thank you

3
  • Unless you are actually testing with CMake 3.10, you should not declare it as your minimum. Oct 11, 2021 at 23:55
  • There is no such source property as CMAKE_C_FLAGS Oct 11, 2021 at 23:55
  • @AlexReinking, the version of CMake on my system is 3.10 that's I declared it as minimum. This is the first blog i referred to (cmake.org/cmake/help/latest/guide/tutorial/…), to get started with CMake. I checked the version of CMake on my system and it was 3.10
    – Raghav
    Oct 12, 2021 at 0:01

1 Answer 1

3

I know that my function "ZCNET_detec_pkt.c" is the one that takes the most amount of time because it contains the main algorithm of my project and I am trying to optimize it. At this moment, I plan to optimize one source file at a time so that it will be easy to debug.

I strongly encourage you to just use the RelWithDebInfo configuration setting.

Setting optimization flags (especially -O3) on a single file will not necessarily make your program as debuggable as you intend and is generally an anti-pattern. On some platforms, mixing "debug" and "release" objects is either difficult or impossible (Windows is one such platform).


To answer your question directly, however, here is a minimal example:

cmake_minimum_required(VERSION 3.21)
project(inadvisable)

add_executable(inadvisable main.cpp src1.cpp)
set_source_files_properties(
  src1.cpp
  PROPERTIES
    COMPILE_OPTIONS "-O3"
)

I can then compile and check the command lines like so:

$ touch main.cpp src1.cpp
$ cmake -G Ninja -S . -B build 
...
$ cmake --build build -- -nv
[1/3] /usr/bin/c++   -g -MD -MT CMakeFiles/inadvisable.dir/main.cpp.o -MF CMakeFiles/inadvisable.dir/main.cpp.o.d -o CMakeFiles/inadvisable.dir/main.cpp.o -c /path/to/main.cpp
[2/3] /usr/bin/c++   -g -O3 -MD -MT CMakeFiles/inadvisable.dir/src1.cpp.o -MF CMakeFiles/inadvisable.dir/src1.cpp.o.d -o CMakeFiles/inadvisable.dir/src1.cpp.o -c /path/to/src1.cpp
[3/3] : && /usr/bin/c++ -g  CMakeFiles/inadvisable.dir/main.cpp.o CMakeFiles/inadvisable.dir/src1.cpp.o -o inadvisable   && :

As you can see, -O3 was applied only to src1.cpp and not to main.cpp.

Not the answer you're looking for? Browse other questions tagged or ask your own question.