alandefreitas/matplotplusplus

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

AXIHIXA opened this issue · 3 comments

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

Bug category

  • bug - compilation error
  • bug - compilation warning
  • bug - runtime error
  • bug - runtime warning
  • bug - logic error

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

  • cross-platform issue - linux
  • cross-platform issue - windows
  • cross-platform issue - macos

Environment Details:

  • OS: ubuntu
  • OS Version: 20.04
  • Compiler: g++ (from ppa:ubuntu-toolchain-r/test)
  • Compiler version: 11.1.0

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

I am also experiencing this issue.

acxz commented

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:

option(MATPLOTPP_BUILD_SHARED_LIBS "Build shared libraries" ${BUILD_SHARED_LIBS})

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?

Probably yes. It's related to this PR: #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.