yse / easy_profiler

Lightweight profiler library for c++
MIT License
2.15k stars 184 forks source link

can't see timings from a shared lib on the same thread #151

Open brendena opened 5 years ago

brendena commented 5 years ago

I'm not able to see the timings that are called from a shared library. I can see the timings if there used in a standard h file or cpp file that are included in the same cmake executible. But if i move them into a separate shared lib i don't see any of the logs. Its probably a cmake issue but my cmake file is really simple and the code compiles and run.

Tested on Ubuntu 18.04 x64 on Release 2.01 and the latest dev branch.

/////////////////////main.cpp
#include <easy/profiler.h>
#include <easy/reader.h>
#include "test1.h"
#include "TestDir/Test2.h"
int main(int argc, char* argv[])
{

    EASY_PROFILER_ENABLE;
    EASY_MAIN_THREAD;
    profiler::startListen();

    test1(); //in header
    test2(); //in separate shared lib

    auto blocks_count = profiler::dumpBlocksToFile("test.prof");

    return 0;
}
/////////////////CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(TestEasyMonitor)
set(CMAKE_CXX_STANDARD 14)

add_subdirectory(${CMAKE_SOURCE_DIR}/TestDir) #adding shared lib TestDir
add_executable(TestEasyMonitor main.cpp )

find_package(easy_profiler REQUIRED)
target_link_libraries(TestEasyMonitor easy_profiler TestDir) #linking shared lib TestDir
/////////////////////test1.h
#ifndef TEST1
#define TEST1
#include <easy/profiler.h>
#include <easy/reader.h>
#include <unistd.h>
void test1(){
    EASY_BLOCK("test1");
    for(int i =0; i < 20; i++){
        EASY_BLOCK("sleep");
        usleep(1000);
    }
}
#endif 

Code from the shared lib from TestDir folder.

///////////////////////TestDir/CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(TestDir)

add_library( TestDir SHARED Test.cpp)

target_link_libraries(TestDir easy_profiler)
/////////////////////TestDir/Test2.h
#ifndef TEST_2
#define TEST_2
#include <easy/profiler.h>
#include <easy/reader.h>
#include <unistd.h>

void test2();

#endif //TEST_2
/////////////////TestDir/Test2.cpp
#include "Test2.h"

void test2(){
    EASY_BLOCK("test2");
    for(int i =0; i < 20; i++){
        EASY_BLOCK("sleep");
        usleep(1000);
    }
}
cas4ey commented 5 years ago

Hi @brendena
You need to define BUILD_WITH_EASY_PROFILER somehow.
For example add

target_compile_definitions(TestDir PRIVATE -DBUILD_WITH_EASY_PROFILER=1)

to the shared library's CMakeLists.txt.

Alternatively you can use PUBLIC visibility for this definition in one of your main projects to avoid adding it into each project.

oguchan commented 5 years ago

I had exact same; issue @cas4ey 's comment helped!