在 Mac 上使用 std::ofstream 时如何解决未定义的符号

问题描述 投票:0回答:1

更新: 我也包括对

ld
的孤立呼叫。并提供第二个示例
main
函数,该函数 does 成功编译、链接和调用
std::
方法。两者都出现在这篇编辑过的帖子的末尾。此外,我尝试提供
-std=c++11 -stdlib=libc++
编译器选项,以及许多其他尝试让
clang
找到/链接正确的库。这些会产生相同的未定义符号错误。

我不明白为什么链接器能够找到

std::out
(在工作示例中)但找不到
std::ofstream
(在失败的示例中)。

原问题描述:

在我的 Mac 上编译,使用时出现未定义符号

std::ofstream
.

我已经重新安装了 XCode 命令行工具,并尝试了我能找到的库路径和开关的每一种排列。我已经尝试使用“clang++”和“g++”来编译此代码,但在我的 Mac(OSX 12.6.3、XCode 14.2 (14C18))上继续出现链接器未定义符号错误。

任何帮助将不胜感激!

这是我的简短 C++ 文件,

foo.cpp

#include <fstream>

int main(int argv, char*[]) 
{
    std::ofstream out("out.txt");
    out << "This is output written to a file\n";
}

这里是我试图编译它:

% g++ foo.cpp
Undefined symbols for architecture x86_64:
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::open(char const*, unsigned int)", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-d774c2.o
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::basic_filebuf()", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-d774c2.o
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::~basic_filebuf()", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-d774c2.o
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::~basic_ofstream() in foo-d774c2.o
  "VTT for std::__1::basic_ofstream<char, std::__1::char_traits<char> >", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-d774c2.o
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::~basic_ofstream() in foo-d774c2.o
  "vtable for std::__1::basic_ofstream<char, std::__1::char_traits<char> >", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-d774c2.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是我尝试用

-v
编译它以显示编译器和链接器选项、搜索路径等:

% g++ -v foo.cpp
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name foo.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-strict-return -fno-rounding-math -funwind-tables=2 -target-sdk-version=13.1 -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -debugger-tuning=lldb -target-linker-version 820.1 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant -Wno-cast-function-type -Wno-bitwise-instead-of-logical -fdeprecated-macro -fdebug-compilation-dir=/Users/ghassett/tmp -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcommon -fcolor-diagnostics -clang-vendor-feature=+messageToSelfInClassMethodIdReturnType -clang-vendor-feature=+disableInferNewAvailabilityFromInit -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -mllvm -disable-aligned-alloc-awareness=1 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/gp/4g1pmgt925gbj7mrwgsjlxbc0000gn/T/foo-55ecc8.o -x c++ foo.cpp
clang -cc1 version 14.0.0 (clang-1400.0.29.202) default target x86_64-apple-darwin21.6.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -platform_version macos 12.0.0 13.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o a.out -L/usr/local/lib /var/folders/gp/4g1pmgt925gbj7mrwgsjlxbc0000gn/T/foo-55ecc8.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::open(char const*, unsigned int)", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-55ecc8.o
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::basic_filebuf()", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-55ecc8.o
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::~basic_filebuf()", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-55ecc8.o
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::~basic_ofstream() in foo-55ecc8.o
  "VTT for std::__1::basic_ofstream<char, std::__1::char_traits<char> >", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-55ecc8.o
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::~basic_ofstream() in foo-55ecc8.o
  "vtable for std::__1::basic_ofstream<char, std::__1::char_traits<char> >", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo-55ecc8.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

隔离链接器命令:

ld
的孤立调用如下所示——请注意,
-lc++
已提供给链接器:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld \
-demangle \
-lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib \
-no_deduplicate \
-dynamic \
-arch x86_64 \
-platform_version macos 12.0.0 13.1 \
-syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk \
-o a.out \
-L/usr/local/lib \
foo.o \
-lc++ \
-lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/lib/darwin/libclang_rt.osx.a

输出是一样的:

Undefined symbols for architecture x86_64:
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::open(char const*, unsigned int)", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo.o
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::basic_filebuf()", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo.o
  "std::__1::basic_filebuf<char, std::__1::char_traits<char> >::~basic_filebuf()", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo.o
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::~basic_ofstream() in foo.o
  "VTT for std::__1::basic_ofstream<char, std::__1::char_traits<char> >", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo.o
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::~basic_ofstream() in foo.o
  "vtable for std::__1::basic_ofstream<char, std::__1::char_traits<char> >", referenced from:
      std::__1::basic_ofstream<char, std::__1::char_traits<char> >::basic_ofstream(char const*, unsigned int) in foo.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64

成功链接并使用std的例子:

此代码编译、链接和执行:

% cat works.cpp
#include <iostream>

int main(int argv, char*[]) 
{
    std::cout << "Writing to the console works\n";
}                                                                                                                                                                   

% g++ works.cpp -o works

% ./works
Writing to the console works

非常感谢您提供的任何帮助!

c++ macos clang ld undefined-symbol
1个回答
0
投票

更新:通过自制软件安装的一些开发人员工具和 XCode 命令行工具之间一定存在冲突。

brew update
brew upgrade
brew doctor
brew unlink llvm
的一些组合已经完成了这个技巧,我的小应用程序现在可以编译和链接。感谢所有提供帮助的人。我意识到我在这里的回答是不完美的,但希望它仍然对下线的人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.