Technical help required

Hi Team,

i am working on a project and created a build system using cmake. I want to use optimization on the selected code c files. Could you please let me know how to achieve that . Please send an example how to achieve -Osize compile flag on my project c files.

Regards
Suraj patel

Hi,

If you’re using a single configuration generator, such as Unix Makefiles (which is the default on Linux and macOS) or Ninja, then you can pass -DCMAKE_BUILD_TYPE=MinSizeRel to cmake when configuring.

If you’re using a multi configuration generator, such as Visual Studio (which is the default on Windows) or Xcode, then you can select the MinSizeRel configuration at build time. For instance, you can pass --config MinSizeRel to cmake --build.

I hope this helps!

Hi,

There are group of .c files in which i need to apply -Ogeneral or some other optimization. I am unable to figure it out how to do so.

Regards,
Suraj

Sorry, I missed the part about applying this optimization flag only on certain files. To achieve that, you can use the function set_source_files_properties to set the COMPILE_OPTIONS property on given files.

I am getting an error

The example is as follows

Dir1>
Hello.c
Hi.c
Dir2>
Test1.c
Test2.c

Now i want to apply optimization on hello.c and test1.c

The code snippet is as follows:

Set(c_sources
${cmake_current_source_dir}/Dir1/hello.c
${cmake_current_source_dir}/Dir2/Test1.c

)

set_source_files(${c_sources} PROPERTIES COMPILE_OPTIONS -Ogeneral)

Could you please suggest why i am getting an error

And the error is…?

But if you’ve posted your code verbatim, then the error is here:

As Alain wrote above, the command is called set_source_files_properties(), not set_source_files().

Side note: the variable CMAKE_CURRENT_SOURCE_DIR is spelled in all uppercase. CMake variable names are case-sensitive.