alandefreitas / matplotplusplus

Matplot++: A C++ Graphics Library for Data Visualization 📊🗾
https://alandefreitas.github.io/matplotplusplus/
MIT License
4.33k stars 333 forks source link

Target std::filesystem was not found (g++11) #334

Open AXIHIXA opened 1 year ago

AXIHIXA commented 1 year ago

Hi there, first of all, thanks so much for this awesome repo.

Bug category

Describe the bug Does not compile when linking against Matplot++:

CMake Error at .../Matplot++Targets.cmake:77 (set_target_properties):
  The link interface of target "Matplot++::matplot" contains:

    std::filesystem

  but the target was not found. 

Steps to Reproduce Built with the latest release with the following commands:

cmake -B build/local \
    -DMATPLOTPP_BUILD_EXAMPLES=OFF \
    -DMATPLOTPP_BUILD_SHARED_LIBS=ON \
    -DMATPLOTPP_BUILD_TESTS=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="$HOME/lib/Matplot++" \
    -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON
cmake --build build/local 
cmake --install build/local

Sample project (CMakeLists.txt) linking against Matplot++:

cmake_minimum_required(VERSION 3.20)
project(Matplotlib++Demo)
set(CMAKE_CXX_STANDARD 20)
find_package(Matplot++ REQUIRED HINTS "$ENV{HOME}/lib/Matplot++/lib/cmake/Matplot++/")
set(EXECUTABLE ${PROJECT_NAME})
add_executable(${EXECUTABLE} src/main.cpp)
target_link_libraries(${EXECUTABLE} Matplot++::matplot)

Output Does not compile, no output.

Platform

Environment Details:

Additional context g++11 does not require a manual link against the filesystem module (it is linked automatically). E.g., the following code compiles and runs correctly on my side:

#include <iostream>
#include <filesystem>

int main(int argc, char * argv[])
{
    std::cout << std::filesystem::current_path() << '\n';
    return 0;
}

without the need for CMake commands in the link_libraries family.

acxz commented 1 year ago

I am also experiencing this issue.

acxz commented 1 year ago

I was able to solve the problem by using -BUILD_SHARED_LIBS=ON \ instead of -DMATPLOTPP_BUILD_SHARED_LIBS=ON \

Looks like the following line is causing the issue: https://github.com/alandefreitas/matplotplusplus/blob/7b757c063ae347bb7f945cb6dacb713418464746/CMakeLists.txt#L43

Maybe should be option(MATPLOTPP_BUILD_SHARED_LIBS "Build shared libraries" OFF)

@alandefreitas is this an actual issue that needs to be fixed or is the current behavior desired?

alandefreitas commented 1 year ago

Probably yes. It's related to this PR: https://github.com/alandefreitas/matplotplusplus/pull/323

Although option(MATPLOTPP_BUILD_SHARED_LIBS "Build shared libraries" ${BUILD_SHARED_LIBS}) is correct. It lets the user customize it but also inherits the default behavior from the global CMake option.

The problem is that the std::filesystem interface target is not being installed in that CMake execution path. We could fix that or... since this is a C++17 library we could get rid of this target and stop supporting compilers that still don't have native std::filesystem support.

This target exists because most compilers back then supported C++17 language features but had not implemented std::filesystem yet.