簡體   English   中英

g++ 編譯錯誤

[英]g++ compile error

我是 Ubuntu 的新手。 我試圖編譯一個簡單的“Hello World!” Ubuntu 11.04 中的 c++ 代碼,使用該代碼(在終端中):

gcc -Wall -W -Werror tex.cpp -o tex. 

但是編譯器返回了很多錯誤:

/tmp/ccL8c1p8.o: In function `main':
tex.cpp:(.text+0xa): undefined reference to `std::cout'
tex.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccL8c1p8.o: In function `__static_initialization_and_destruction_0(int, int)':
tex.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
tex.cpp:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status
mohrd@mio:~$ gcc -Wall -W -Werror tex.cpp -o tex.
/tmp/ccEke5JP.o: In function `main':
tex.cpp:(.text+0xa): undefined reference to `std::cout'
tex.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccEke5JP.o: In function `__static_initialization_and_destruction_0(int, int)':
tex.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
tex.cpp:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
collect2: ld returned 1 exit status

簡單的代碼:

#include <algorithm>
#include <iostream>
using namespace std;

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

為什么? 我該怎么辦???

非常感謝 ...

您需要使用 g++(或 c++)命令來編譯您的程序,由於 .cpp 擴展名而不是所需的 c++ 庫中的鏈接,因此使用“gcc”會將其編譯為 c++。

使用 g++ 而不是 gcc 來編譯 C++ 代碼:

g++ -Wall -W -Werror tex.cpp -o tex

默認情況下,gcc 不鏈接 stdc++ 庫,這是您的代碼所必需的。

使用g++命令而不是gcc

嘗試:

g++ -Wall hello.cpp -o hello

因為您正在嘗試編譯C++代碼,而不是C代碼!

-Wall表示Warning ALL ,因此您已經看到所有警告

此外,請有意義地命名您的程序和源文件!

LE:當然-Werror仍然是您的選擇

LE2:您的代碼中不需要#include <algorithm> ,因此您可以將其刪除!

我在創建一個巨大的數組 (50005) 項目時遇到了同樣的錯誤。 當心

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM