
D1X
Soll das das Beispiel sein:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
Es wirft den Fehler:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.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*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Auch dieses Beispiel:
#include <iostream>
int main()
{
std::cout << "Hola, moondo.\n";
}
wirft den Fehler:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.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*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)': main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Hinweis: Ich verwende Debian 7 (Wheezy).

Shauryachats
Kompilieren Sie das Programm mit:
g++ -Wall -Wextra -Werror -c main.cpp -o main.o
^^^^^^^^^^^^^^^^^^^^ <- For listing all warnings when your code is compiled.
als cout
ist in der C++-Standardbibliothek vorhanden, die benötigt würde explizite Verlinkung mit -lstdc++
beim Benutzen gcc
; g++
bindet standardmäßig die Standardbibliothek ein.
Mit gcc
(g++
sollte gegenüber bevorzugt werden gcc
)
gcc main.cpp -lstdc++ -o main.o

AB
Ja, verwenden g++
Befehl hat bei mir funktioniert:
g++ my_source_code.cpp

losnihciL
Vorausgesetzt code.cpp
der Quellcode ist, wird Folgendes keine Fehler auslösen:
make code
./code
Hier kompiliert der erste Befehl den Code und erstellt eine ausführbare Datei mit demselben Namen, und der zweite Befehl führt sie aus. Eine Angabe ist nicht erforderlich g++
Stichwort in diesem Fall.
Makefiles
Wenn Sie mit einem Makefile arbeiten und wie ich hier gelandet sind, dann ist dies wahrscheinlich das, was Sie suchen, oder:
Wenn Sie ein Makefile verwenden, müssen Sie es ändern cc
Wie nachfolgend dargestellt
my_executable : main.o
cc -o my_executable main.o
zu
CC = g++
my_executable : main.o
$(CC) -o my_executable main.o

Netzhaut
FWIW, wenn Sie ein Makefile möchten, können Sie wie folgt antworten, indem Sie den Compiler oben wechseln.
# links stdc++ library by default
# CC := g++
# or
CC := cc
all: hello
util.o: util.cc
$(CC) -c -o util.o util.cc
main.o: main.cc
$(CC) -c -o main.o main.cc
# notice -lstd++ is after the .o files
hello: main.o util.o
$(CC) -o hello main.o util.o -lstdc++
clean:
-rm util.o main.o hello
10134600cookie-checkFehler “undefinierter Verweis auf ‘std::cout'”yes
Versuchen
g++
anstattgcc
.gcc
ist für C und gibt Ihnen keinen Zugriff auf die C++-Standardbibliothek.– Juanchopanza
30. Januar 2015 um 13:21 Uhr
Nun, das hat das Problem definitiv gelöst. Soweit ich weiß, ist GCC das Akronym für Gnu Compiler Collection. Sollte es nicht bei Bedarf den g++ Compiler aufrufen? Der Befehl gcc ruft also stattdessen den c-Compiler auf …
– D1X
31. Januar 2015 um 14:42 Uhr
@D1X liegt daran, dass Sie den Linker separat vom Compiler aufgerufen haben. wenn du schreibst
gcc -o edit main.o
das weiß es nichtmain.o
wird C++-Startbibliotheken benötigen.– MM
26. Februar 2015 um 4:22 Uhr
RTFM gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html
– Jonathan Wakely
4. Juni 2015 um 14:11 Uhr
F: Sollte es nicht bei Bedarf den g++ Compiler aufrufen? A: Nicht mehr als gcc sollte gfortran, gjc, … usw. usw. nach Bedarf aufrufen.
– paulsm4
16. Juli 2015 um 3:29 Uhr