C ++ Primer (fifth edition)

Possible duplicate:
Can't find the standard GCC library?

I am trying to integrate with this C ++ book I got for the holidays, I come from a limited understanding of python, so this material is very strange for me.

I printed this code from one of the first lessons in a text editor and saved it as a .cpp file.

#include <iostream> int main() { std::cout << "Enter two numbers:" << std::endl; int v1 = 0, v2 = 0; std::cin >> v1 >> v2; std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; return 0; } 

but my terminal gives this crazy conclusion when I try to compile it, what happens?

 Raymond-Weisss-MacBook-Pro:c++ Raylug$ gcc prog2.cpp Undefined symbols for architecture x86_64: "std::basic_istream<char, std::char_traits<char> >::operator>>(int&)", referenced from: _main in cckdLEun.o "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 cckdLEun.o "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from: _main in cckdLEun.o "std::ios_base::Init::Init()", referenced from: __static_initialization_and_destruction_0(int, int)in cckdLEun.o "std::ios_base::Init::~Init()", referenced from: ___tcf_0 in cckdLEun.o "std::cin", referenced from: _main in cckdLEun.o "std::cout", referenced from: _main in cckdLEun.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 cckdLEun.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 cckdLEun.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status Raymond-Weisss-MacBook-Pro:c++ Raylug$ 
+4
source share
2 answers

Yes, you should use g++ to compile C ++ instead of gcc , but you should probably consider the answer as you say you're new to using compilers.

GCC stands for GNU Compiler Collection and is a collection of programs that can be used to compile source code for different languages. The gcc command provides an external interface for users to compile their programs. The gcc command will try to determine the language you want to compile from the file extensions in the source files. For example, a file named hello.c will be compiled as C, a file named foo.cpp will be compiled as C ++, and a file named bar.m will compile as Objective-C.

You can give the -x option to explicitly specify which language to compile, regardless of the file extension. For example, gcc -x c++ main.c will compile main.c as a C ++ file, despite the extension .c .

However, although gcc will determine that your files are C ++, by default it will still not reference the standard C ++ library. This library should be related to compiling nothing but the simplest C ++ files. You can link to the GCC implementation of the standard library by adding -lstdc++ to your options. Therefore gcc -lstdc++ prog2.cpp should work fine.

However, for convenience, a different command is provided for C ++ programmers. Namely, g++ . The g++ command will automatically reference the standard C ++ library, so you do not need to do this explicitly. It also calls .c , .h and .i files to process C ++ files.

If you use gcc sequentially to compile C and g++ , you should have no problem.

+5
source

You need to use g ++ instead of gcc

+1
source

All Articles