Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Conan Flag for profiling #761

Open
vlocateli opened this issue Jan 7, 2025 · 4 comments · May be fixed by #773
Open

Add Conan Flag for profiling #761

vlocateli opened this issue Jan 7, 2025 · 4 comments · May be fixed by #773
Assignees

Comments

@vlocateli
Copy link
Contributor

vlocateli commented Jan 7, 2025

Add a flag in the Conan for use in profiling, since it will be needed to compare speeds between OpenMP implementations and stdlibc++ thread implementation.

@damskii9992
Copy link
Contributor

To clarify, what you would like is a flag to build/compile the application with either OpenMP or stdlibc++?

@anderskaestner
Copy link
Member

I think what he means is
To add compiler flags that enable profiling information to the built items in a CMake project, you can follow these steps:

General Approach

  1. Add the Flags Globally
    You can set compiler flags globally for all targets in your project using CMAKE_CXX_FLAGS or CMAKE_C_FLAGS in your CMakeLists.txt:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
  2. Add the Flags for Specific Targets
    You can also add the flags only for specific targets using target_compile_options:

    target_compile_options(your_target_name PRIVATE -pg)
    target_link_options(your_target_name PRIVATE -pg)

Step-by-Step Example

Here’s an example of how you might do it in practice:

Global Profiling Flags

cmake_minimum_required(VERSION 3.10)
project(ProfilingExample)

# Add profiling flags globally
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")

# Add an executable
add_executable(example main.cpp)

Per-Target Profiling Flags

cmake_minimum_required(VERSION 3.10)
project(ProfilingExample)

# Add an executable
add_executable(example main.cpp)

# Add profiling flags to the specific target
target_compile_options(example PRIVATE -pg)
target_link_options(example PRIVATE -pg)

Explanation of the -pg Flag

The -pg compiler flag enables profiling information for tools such as gprof. Adding it to both compile and link stages ensures the profiling data is properly embedded in the resulting binaries.

Verify the Profiling Information

After building your project, run the executable to generate profiling data (usually a gmon.out file). Analyze it with gprof:

gprof ./example gmon.out > analysis.txt

Let me know if you need help integrating these steps into your project!

@vlocateli
Copy link
Contributor Author

vlocateli commented Jan 8, 2025

@damskii9992 something like Anders wrote with something like this:

<Conan command> <Enable OpenMP> <Use profiling flag>
<Conan command> <Enable stdlibc++ thread> <Use profiling flag>

Also, could you do a flag to enable address sanitizers?

@damskii9992 damskii9992 linked a pull request Jan 29, 2025 that will close this issue
@damskii9992
Copy link
Contributor

Hello Victor
I have just opened a small PR to show how flags such as these can be passed to the compiler when building with conan:
#773
The method is simply to create a new profile with the flags under the [conf] section and then to use this profile when running Conan.
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Project tasks
Development

Successfully merging a pull request may close this issue.

3 participants