Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Very simple program not working in c++?

Tags:

c++

cout

I can't figure out why this isn't working...

I am working in Linux

g++ doesn't do anything

gcc prints the following:

/tmp/ccyg7NDd.o: In function `main':
test.cc:(.text+0x14): undefined reference to `std::cout'
test.cc:(.text+0x19): 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*)'
test.cc:(.text+0x21): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cc:(.text+0x29): undefined reference to `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> >&))'
/tmp/ccyg7NDd.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cc:(.text+0x51): undefined reference to `std::ios_base::Init::Init()'
test.cc:(.text+0x56): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccyg7NDd.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Code:

#include<iostream>
#include<stdio.h>

int main(){

std::cout<<"test "<<std::endl;
return 0;

};
like image 717
spatara Avatar asked Feb 25 '12 19:02

spatara


People also ask

Why is my C program not showing output?

try adding a \n at the end of the printf statement. or, fflush(stdout) after the current printf.

Why my C program is not running in VS code?

Go to the menu Code > Preferences > Settings. In the User tab on the left panel, expand the Extensions section. Find and select Run Code Configuration. Find and check the box Run in Terminal.

Is C programming hard for beginners?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

gcc main.cpp -lstdc++

or

g++ main.cpp
like image 75
kev Avatar answered Sep 21 '22 14:09

kev


gcc is the C compiler, you need to use g++ (or use gcc with option -lstdc++ as pointed out by others). If by nothing is printed after you use g++ is what you mean, you have to execute the compiled binary after you build it (i.e. when g++ completes).

main.cpp:

#include<iostream>

int main(){

std::cout<<"test "<<std::endl;
return 0;

};

Build:

g++ main.cpp -o main

Execute:

./main

Output:

test 
like image 27
hmjd Avatar answered Sep 21 '22 14:09

hmjd