fmtlib / fmt

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

Avoid compiling static library when using `FetchContent` with `fmt::fmt-header-only` #4022

Closed filippobistaffa closed 2 weeks ago

filippobistaffa commented 2 weeks ago

My goal is to avoid compiling libfmt.a with the following CMakeList.txt and test.cpp

cmake_minimum_required(VERSION 3.20)
project(test)

include(FetchContent)
FetchContent_Declare(fmt
    GIT_REPOSITORY https://github.com/fmtlib/fmt.git
    GIT_TAG master
)
FetchContent_MakeAvailable(fmt)

add_executable(test test.cpp)
target_link_libraries(test PRIVATE fmt::fmt-header-only)
#include <fmt/core.h>

int main() {

    fmt::print("Hello world!\n");
    return 0;
}

Is it possible to achieve this? What I'm getting instead is the following:

[ 20%] Building CXX object _deps/fmt-build/CMakeFiles/fmt.dir/src/format.cc.o
[ 40%] Building CXX object CMakeFiles/test.dir/test.cpp.o
[ 60%] Building CXX object _deps/fmt-build/CMakeFiles/fmt.dir/src/os.cc.o
[ 80%] Linking CXX executable test
[100%] Linking CXX static library libfmt.a
[100%] Built target fmt
[100%] Built target test
vitaut commented 2 weeks ago

I am not sure, you might want to check the CMake docs. In general, the header-only config is not recommended and there are even fewer reason to use it with FetchConfig.

filippobistaffa commented 2 weeks ago

In general, the header-only config is not recommended and there are even fewer reason to use it with FetchConfig.

Do you mean using fmt::fmt-header-only in CMake or using #define FMT_HEADER_ONLY? Also, why is it "not recommended" (not that I don't believe you, just curious if there's any technical drawback).

vitaut commented 2 weeks ago

Do you mean using fmt::fmt-header-only in CMake or using #define FMT_HEADER_ONLY?

Both.

Header-only is generally bad for compile times, see e.g. https://stackoverflow.com/a/68020236/471164.