C++ fmtlib:"Undefined reference" 构建和#include <> 这个库后出错
C++ fmtlib: "Undefined reference" error after building and #include <> this library
我从 https://github.com/fmtlib/fmt and then executed the following commands from official documentation https://fmt.dev/latest/usage.html 下载了库:
mkdir build
cd build
cmake ..
sudo make install
命令执行无误。 sudo make install
命令的最终输出:
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/libfmt.a
-- Installing: /usr/local/include/fmt/args.h
-- Installing: /usr/local/include/fmt/chrono.h
-- Installing: /usr/local/include/fmt/color.h
-- Installing: /usr/local/include/fmt/compile.h
-- Installing: /usr/local/include/fmt/core.h
-- Installing: /usr/local/include/fmt/format.h
-- Installing: /usr/local/include/fmt/format-inl.h
-- Installing: /usr/local/include/fmt/locale.h
-- Installing: /usr/local/include/fmt/os.h
-- Installing: /usr/local/include/fmt/ostream.h
-- Installing: /usr/local/include/fmt/printf.h
-- Installing: /usr/local/include/fmt/ranges.h
-- Installing: /usr/local/include/fmt/xchar.h
-- Installing: /usr/local/lib/cmake/fmt/fmt-config.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-config-version.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-targets.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-targets-release.cmake
-- Installing: /usr/local/lib/pkgconfig/fmt.pc
接下来我创建了包含以下内容的文件 main.cpp
:
#include <fmt/core.h>
int main()
{
fmt::print("Hello");
return 0;
}
并用命令g++ main.cpp -o main
编译了它。
但是,编译结果出现以下错误:
/usr/bin/ld: /tmp/ccJ3FzHH.o: in the function «main»:
main.cpp:(.text+0x8a): undefined reference to «fmt::v8::vprint(fmt::v8::basic_string_view<char>, fmt::v8::basic_format_args<fmt::v8::basic_format_context<fmt::v8::appender, char> >)»
collect2: error: ld returned 1 exit status
我正在使用 Ubuntu 20.04
在编译命令中添加参数-lfmt
可以解决问题。
-l<name>
参数通常代表将库 <name>
包含到编译中(参见 GCC documentation)
命令如下所示:
$ g++ main.cpp -lfmt -o main
我从 https://github.com/fmtlib/fmt and then executed the following commands from official documentation https://fmt.dev/latest/usage.html 下载了库:
mkdir build
cd build
cmake ..
sudo make install
命令执行无误。 sudo make install
命令的最终输出:
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/libfmt.a
-- Installing: /usr/local/include/fmt/args.h
-- Installing: /usr/local/include/fmt/chrono.h
-- Installing: /usr/local/include/fmt/color.h
-- Installing: /usr/local/include/fmt/compile.h
-- Installing: /usr/local/include/fmt/core.h
-- Installing: /usr/local/include/fmt/format.h
-- Installing: /usr/local/include/fmt/format-inl.h
-- Installing: /usr/local/include/fmt/locale.h
-- Installing: /usr/local/include/fmt/os.h
-- Installing: /usr/local/include/fmt/ostream.h
-- Installing: /usr/local/include/fmt/printf.h
-- Installing: /usr/local/include/fmt/ranges.h
-- Installing: /usr/local/include/fmt/xchar.h
-- Installing: /usr/local/lib/cmake/fmt/fmt-config.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-config-version.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-targets.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-targets-release.cmake
-- Installing: /usr/local/lib/pkgconfig/fmt.pc
接下来我创建了包含以下内容的文件 main.cpp
:
#include <fmt/core.h>
int main()
{
fmt::print("Hello");
return 0;
}
并用命令g++ main.cpp -o main
编译了它。
但是,编译结果出现以下错误:
/usr/bin/ld: /tmp/ccJ3FzHH.o: in the function «main»:
main.cpp:(.text+0x8a): undefined reference to «fmt::v8::vprint(fmt::v8::basic_string_view<char>, fmt::v8::basic_format_args<fmt::v8::basic_format_context<fmt::v8::appender, char> >)»
collect2: error: ld returned 1 exit status
我正在使用 Ubuntu 20.04
在编译命令中添加参数-lfmt
可以解决问题。
-l<name>
参数通常代表将库 <name>
包含到编译中(参见 GCC documentation)
命令如下所示:
$ g++ main.cpp -lfmt -o main