fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
19.84k stars 2.42k forks source link

Header-only mode not linking with fmt::ostream #3952

Closed breyerml closed 1 month ago

breyerml commented 2 months ago

Fmt fails to link in header-only mode if fmt::ostream is used on the last release and the current master.

Minimal example to reproduce:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.1...3.29)

project(
  test
  VERSION 1.0
  LANGUAGES CXX)

add_executable(prog main.cpp)

include(FetchContent)
FetchContent_Declare(fmt
                     GIT_REPOSITORY https://github.com/fmtlib/fmt.git
                     GIT_TAG 57593a1
                     QUIET
                     EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(fmt)
target_link_libraries(prog PUBLIC fmt::fmt-header-only)

main.cpp:

#include "fmt/os.h"

int main() {
  fmt::ostream out = fmt::output_file("test.txt");
  out.print("foo");
  return 0;
}

Compiled via:

cmake -B build .
cmake --build build

results in the linker error:

[ 20%] Building CXX object CMakeFiles/prog.dir/main.cpp.o
[ 40%] Linking CXX executable prog
/usr/bin/ld: CMakeFiles/prog.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x80): undefined reference to `fmt::v10::ostream::~ostream()'
/usr/bin/ld: main.cpp:(.text+0xa6): undefined reference to `fmt::v10::ostream::~ostream()'
/usr/bin/ld: CMakeFiles/prog.dir/main.cpp.o: in function `fmt::v10::ostream::ostream(fmt::v10::basic_cstring_view<char>, fmt::v10::detail::ostream_params const&)':
main.cpp:(.text._ZN3fmt3v107ostreamC2ENS0_18basic_cstring_viewIcEERKNS0_6detail14ostream_paramsE[_ZN3fmt3v107ostreamC5ENS0_18basic_cstring_viewIcEERKNS0_6detail14ostream_paramsE]+0x2b): undefined reference to `fmt::v10::detail::file_buffer::file_buffer(fmt::v10::basic_cstring_view<char>, fmt::v10::detail::ostream_params const&)'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/prog.dir/build.make:97: prog] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/prog.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2

Tested with GCC 11.4.0, icpx 2024.0.2, and nvc++ 24.3-0.

vitaut commented 1 month ago

Same as #3708.