c++ - 标准库和GCC的CLion链接器错误

标签 c++ gcc compiler-errors clion undefined-symbol

我正在上一个类,要求使用GCC作为编译器。我正在尝试确保CLion使用的是GCC,但无论我做什么,在尝试运行文件时都会出现以下错误:

Scanning dependencies of target Sandbox
[ 50%] Building CXX object CMakeFiles/Sandbox.dir/main.cpp.o
[100%] Linking CXX executable Sandbox
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:
      printSquare(double) in main.cpp.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(double)", referenced from:
      printSquare(double) 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:
      printSquare(double) 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:
      printSquare(double) 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:
      printSquare(double) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[3]: *** [Sandbox] Error 1
make[2]: *** [CMakeFiles/Sandbox.dir/all] Error 2
make[1]: *** [CMakeFiles/Sandbox.dir/rule] Error 2
make: *** [Sandbox] Error 2

这是我用来测试GCC的非常简单的main.cpp文件:
#include <iostream>

using namespace std;

double square(double x) {
    return x * x;
}

void printSquare(double x) {
    cout << "The square of " << x << " is " << square(x) << endl;
}

int main() {
    printSquare(10.9);
}

以及我的CMakeLists.txt文件:
project(Sandbox)

set(CMAKE_CXX_STANDARD 14)

add_executable(Sandbox main.cpp)

在首选项中,在“构建”,“执行”,“部署”下|工具链| C++编译器,路径为/usr/local/bin/gcc-9

我尝试使用Homebrew更新并重新安装GCC,但在网站上找不到其他类似问题。我在MacOS Catalina 10.15.2上运行。感谢您的帮助。

最佳答案

确保在“设置”->“构建,执行,部署”->“C++编译器”下定位g++编译器(例如:/opt/local/bin/g++)。您需要将g++编译器用于C++代码,而gcc编译器用于C代码,这就是为什么您会因C++标准库函数而出现那些链接器错误的原因。

关于c++ - 标准库和GCC的CLion链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59957140/

相关文章:

java - 使用 STL/POSIX 将二进制文件从 J2SE 加载到 iOS iphone

c - GCC 生成文件不接受 -std=c99 -lm

c - 无效声明 (2)

C++OpenGL : Ray Trace Shading Isn't Properly Shading

c++ - C/C++ 字节顺序和 tcp 套接字

gcc - 如何使用 Intel 语法内联汇编在 GCC 中设置变量?

在 GCC 中动态创建 va_list - 可以做到吗?

compiler-errors - Arduino初学者问题

c++ - VC++6.0 : too many actual parameters

c++ - OpenCV:cv::mat 是否像 shared_ptr?