GCC component cannot find standard library?

I am developing a school project in Xcode. The final product should be sent to the source code with the make file, so I wrote a make file and started compiling this path to make sure I have a working copy. Here is my makefile:

all: main.o StackList.o world.o Farm.o gcc main.o StackList.o world.o Farm.o -g -o Project1 main.o: gcc -g -c main.cpp StackList.o: gcc -g -c Stacklist.cpp world.cpp: gcc -g -c world.cpp Farm.cpp: gcc -g -c Farm.cpp clean: rm *.o Project1 

Compiling each of the object files works fine, but when it switches to "everything", the linking stage does not seem to know the standard library. I get an error of "undefined characters" for each of "cin", "basic_string", "operator new".

I got the impression that these things did not need to be specified directly, but in fact it was not necessary to do this in the past.

Any idea what could happen?

EDIT:

If this helps, here is the start of a (very long) error message:

 Undefined symbols for architecture x86_64: "std::cin", referenced from: _main in main.o "std::cout", referenced from: _main in main.o Farm::print(int) in Farm.o "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from: _main in main.o "std::ios_base::Init::Init()", referenced from: __static_initialization_and_destruction_0(int, int)in main.o __static_initialization_and_destruction_0(int, int)in StackList.o __static_initialization_and_destruction_0(int, int)in world.o __static_initialization_and_destruction_0(int, int)in Farm.o "std::ios_base::Init::~Init()", referenced from: ___tcf_0 in main.o ___tcf_0 in StackList.o ___tcf_0 in world.o ___tcf_0 in Farm.o "operator new(unsigned long)", referenced from: doStackSearch(std::basic_istream<char, std::char_traits<char> >*, std::list<Farm*, std::allocator<Farm*> >*&)in world.o 
+4
source share
3 answers

To link C ++ code, you need to use the g++ command, not the gcc command.

When compiling, the gcc command knows that it compiles C ++ code due to the suffix .cpp (but in any case, it is recommended to use the g++ command).

When linked, the gcc command only sees a bunch of .o files. The g++ command differs from the gcc command in that it knows where to find C ++ specific libraries.

(The fact that the gcc name refers to both the command commonly used to compile C code and the assembly of GNU compilers in general can be a bit confusing.)

+19
source

You need to use g++ to compile and link to C ++ source code, not gcc .

In addition, you can leave all the goals except the first and last. make knows how to compile .cpp files into .o already - you don’t need to tell how to do it.

+1
source
  gcc main.o StackList.o world.o Farm.o -g -o Project1 

What on this command line do you think gcc refers to the C ++ standard library? There are no related C ++ files. You did not specify a language. And you call the compiler like gcc .

-2
source

Source: https://habr.com/ru/post/1268886/


All Articles