Mac(High Sierra)gcc问题(ld:找不到体系结构x86_64的符号)

时间:2018-09-02 16:37:08

标签: macos gcc

我写了非常简单的代码:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

但是当我尝试编译它时,出现了错误:

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/user/CLionProjects/NewMath/cmake-build-debug --target NewMath -- -j 2
[ 50%] Linking CXX executable NewMath
Undefined symbols for architecture x86_64:
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
      _main in main.cpp.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in main.cpp.o
  "std::ios_base::Init::~Init()", referenced from:
      __static_initialization_and_destruction_0(int, int) in main.cpp.o
  "std::cout", referenced from:
      _main in main.cpp.o
  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      _main in main.cpp.o
  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/NewMath.dir/build.make:84: NewMath] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/NewMath.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/NewMath.dir/rule] Error 2
gmake: *** [Makefile:118: NewMath] Ошибка 2

我使用: -Clion -gcc8 -gmake

没有Clion错误仍然存​​在

能帮忙吗

1 个答案:

答案 0 :(得分:1)

您的代码是C ++,但是gcc是C编译器。使用g++g++-7g++-8

g++-7 main.cpp -o main