I'm a beginner to C++ and am trying to compile and use the fmt library in a basic program, but I'm having a problem getting it working. The program, Tester.cpp, is simply as follows:
// #define FMT_HEADER_ONLY
// #include <C:\Program Files (x86)\FMT\include\fmt\format.h>
#include <fmt/format.h>
#include <iostream>
int main() {
std::cout << "C++ Code Executing!" << std::endl;
int num = 7;
std::cout << fmt::format("The answer is {}.", num) << std::endl;
}
I have never really used cmake before, but have taken the following steps to download and compile the library so far:
git clone https://github.com/fmtlib/fmt.git
cd fmt
mkdir build
cd build
cmake ..
cmake --build . --config Release
cmake --install .
This stores a range of header files into this directory:
C:\Program Files (x86)\FMT\include\fmt
As per the commented lines in my program, if I instruct it to use header only and include a pathway directly to the header file it does seem to work, however I believe I should be able compile the fmt library in a way that I can link to it without using header only, ideally as a static library, (though as a beginner I may be wrong on this). In it's current format if I compile with clang++ in the following way:
clang++ -isystem "C:\Program Files (x86)\FMT\include" Tester.cpp -o Tester.exe
I get the following error:
Tester-333c8c.o : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl fmt::v8::vformat(class fmt::v8::basic_string_view<char>,class fmt::v8::basic_format_args<class fmt::v8::basic_format_context<class fmt::v8::appender,char> >)" (?vformat@v8@fmt@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$basic_string_view@D@12@V?$basic_format_args@V?$basic_format_context@Vappender@v8@fmt@@D@v8@fmt@@@12@@Z) referenced in function main
Tester.exe : fatal error LNK1120: 1 unresolved externals
clang++: error: linker command failed with exit code 1120 (use -v to see invocation)
I believe this is because the compiler can find the header files for compilation, but the linker has no definition/object file to link into an executable? This is the part I am trying to fix, but have been unable to do so.
As per the instructions for fmt 8.1.1 I have tried using cmake with -DCMAKE_POSITION_INDEPENDENT_CODE=TRUE to create a static library, resulting in a fmt.lib file, but trying to link to that doesn't seem to help resolve my issues either. I've also tried adding an -L flag to different directories when compiling, but still get the error.
Question
Is someone able to point out where I'm going wrong in trying to compile and use this library for my program?
I am on a Windows system. I have been using clang and Neovim for code editing, though I do have Visual Studio installed as I believe clang relies on part of the instillation. I'm ideally looking to do this purely through command line tools if possible. As a beginner I'm also a bit shaky on how some parts of cpp, such as header files, are supposed to work, should it be relevant.
EDIT As per HolyBlackCat's comment on linking I have also tried the following:
clang++ -isystem "C:\Program Files (x86)\FMT\include" -LC:/FMT -lfmt Tester.cpp -o Tester.exe
with fmt.lib located in C:\FMT and get and error that starts like so:
fmt.lib(format.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in Tester-6086ae.o
msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: __cdecl std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QEAA@H@Z) already defined in libcpmt.lib(xlock.obj)
...
...
Many lines...
...
LINK : warning LNK4217: symbol '_isatty' defined in 'libucrt.lib(isatty.obj)' is imported by 'fmt.lib(format.obj)' in function '"void __cdecl fmt::v8::detail::print(struct _iobuf *,class fmt::v8::basic_string_view<char>)" (?print@detail@v8@fmt@@YAXPEAU_iobuf@@V?$basic_string_view@D@23@@Z)'
Tester.exe : fatal error LNK1169: one or more multiply defined symbols found
clang++: error: linker command failed with exit code 1169 (use -v to see invocation)