Compaile / ctrack

A lightweight, high-performance C++ benchmarking and tracking library for effortless function profiling in both development and production environments. Features single-header integration, minimal overhead, multi-threaded support, customizable output, and advanced metrics for quick bottleneck detection in complex codebases.
MIT License
134 stars 5 forks source link

Missing cmath include, certain Linux linking issues #3

Closed lamweilun closed 2 months ago

lamweilun commented 2 months ago

Please add #include <cmath> as its complaining that its missing references to std::pow and std::fpclassify

Also on a separate note, on Linux, I have to manually link Intel TBB in my CMake. Maybe you could bundle it together in your CMakeLists?

Below is my example, using CPM.cmake, to get things to compile and link. Tested with gcc-14 and clang-18

# CMakeLists.txt
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

file(
  DOWNLOAD
  https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.2/CPM.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

project(cpm)

CPMAddPackage("gh:Compaile/ctrack#v1.0.0")

find_package(TBB REQUIRED)

add_executable(
  ${PROJECT_NAME}
  main.cpp
)

target_compile_features(
  ${PROJECT_NAME} 
  PRIVATE 
  cxx_std_20
)

target_link_libraries(
  ${PROJECT_NAME}
  PRIVATE
  TBB::tbb
  ctrack
)
// main.cpp
#include <iostream>

#include <cmath> // Needed for std::pow and std::fpclassify
#include <ctrack.hpp>

void printFunc() {
  CTRACK;
  std::cout << "Print" << std::endl;
}

int main() {
  printFunc();
  ctrack::result_print();
  return 0;
}
Compaile commented 2 months ago

thanks for the report, we are looking into this

Compaile commented 2 months ago

Since not all compilers support parallel execution without linking to TBB, we add a flag to fall back to sequential calculation of the results. The CMake in the example folder also shows how to link to TBB (also added it to the README as a note). If a user is on a compiler which would need TBB for parallel execution but does not want to include TBB, we added a fallback with a sequential mode.

That way each user can decide if they want to link to tbb (if needed at all to execute parallel c++17 algorithms).

lamweilun commented 2 months ago

@Compaile

Is it not possible to default target_compile_definitions for CTRACK_DISABLE_EXECUTION_POLICY if TBB is not detected?

So instead of having the user to manually set CTRACK_DISABLE_EXECUTION_POLICY, the user should be able to explicitly define CTRACK_DISABLE_EXECUTION_POLICY to disable parallel execution, but for all other cases, it should default to ON if TBB_FOUND is false

EDIT: Also I tried v1.0.1, I have TBB installed, but it still failed to link TBB automatically. So I think what can be done is that, TBB should be linked automatically if FOUND, else CTRACK_DISABLE_EXECUTION_POLICY should be defined if TBB is not found. User should be able to explicitly define CTRACK_DISABLE_EXECUTION_POLICY to override the behaviour above

Compaile commented 2 months ago

That is exactly whats happening at the moment.

if tbb is needed (non msvc) it checks if it can be found, if yes it will link. if no it will set the compile definition.

If it fails to link it automatically on your system please clear the cmake cache and provide the output of cmake ..

lamweilun commented 2 months ago

I just tested v1.0.1, clean build, deleted cache. And this is the result I got:

# CmakeLists.txt
cmake_minimum_required(VERSION 3.28 FATAL_ERROR)

file(
  DOWNLOAD
  https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.2/CPM.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

project(main)

CPMAddPackage("gh:Compaile/ctrack@1.0.1")

add_executable(
  ${PROJECT_NAME}
  main.cpp
)

target_link_libraries(
  ${PROJECT_NAME}
  PRIVATE
  ctrack
)

target_compile_features(
  ${PROJECT_NAME}
  PRIVATE
  cxx_std_17
)
// main.cpp
#include <ctrack.hpp>

int main() {
  CTRACK;
  ctrack::result_print();
  return 0;
}

And I got the output that says a bunch of undefined reference to tbb functions.

To fix it, I had to explicitly add find_package(TBB) and TBB::tbb in the linked libraries.

For reference: I am on CachyOS (Arch Linux distro), TBB is installed as a system package, compiled using clang-18.

Compaile commented 2 months ago

ah i see you dont build the examples, you build your project directly.

in the examples we added a cmake file which does exactly what you want/describe https://github.com/Compaile/ctrack/blob/main/examples/CMakeLists.txt

I am unsure if we can implicit force this in the cmake bundle already or have to require the cmake user to check it themself (they can copy/use the macro from the example files in their cmake project)

Compaile commented 2 months ago

i will prepare another branch to see if i can add this behavior automatically

lamweilun commented 2 months ago

Oh I actually didn't know this, but I did build the examples as well, but it's not working for me.

For what's worth, I did try modifying the root CMakeLists.txt in the repo, I added these lines after Line 8 and it seems to be working.

# Right after add_library(ctrack INTERFACE)
find_package(TBB)
if (TBB_FOUND)
  target_link_libraries(ctrack INTERFACE TBB::tbb)
else()
  target_compile_definitions(ctrack INTERFACE CTRACK_DISABLE_EXECUTION_POLICY)
endif()

I am not sure if this would be the intended way of doing things, but it could be worth a try

Compaile commented 2 months ago

can u test https://github.com/Compaile/ctrack/tree/improve_tbb_interfacing and confirm if it works?

lamweilun commented 2 months ago

^ yup seems to work! Could you also add an option to disable building of examples?

Compaile commented 2 months ago

option added and 1.0.2 released