*.cpp vs Main.cpp

I am a student who is turning in homework (no, I'm not trying to get you guys to do my homework for me). When I compile my code in Linux Mint, I use the terminal command:
g++ -std=c++11 Main.cpp

my TA uses:
g++ -std=c++11 *.cpp

When I compile using my command, no problems. But when I compile using his command, I get an error:
/tmp/ccf6C4Rx.o: In function `ShellSorter::ShellSort(std::vector<Item, std::allocator<Item> >&, int)':
ShellSort.cpp:(.text+0x0): multiple definition of `ShellSorter::ShellSort(std::vector<Item, std::allocator<Item> >&, int)'
/tmp/ccqq9ape.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status

I can post my code here if you guys want, but I was just wondering in general if anybody knew anything about this type of error.
Hi, and welcome to cplusplus :+)

What else is in that folder, no links to anything else? It's finding code in the /temp directory, hopefully that is not where your code is?

Btw you should compile with a high level of warnings:

g++ -std=c++11 -Wall -Wextra -pedantic-errors *.cpp -o MyProgName

You can substitute MyProgName with whatever you want to call your program, it's better than running the default ./a.out

It's worth reading the compiler manual, there are even more options that aren't enabled by the above command. I know there are a zillion lines, but it is still worth it IMO.

If you have multiple files, you are better off with using some kind of make file, but that may be a bit advanced right now.
*.cpp expands to all the filenames ending with .cpp in the current directory.

If you have three files named Main.cpp, Foo.cpp and Bar.cpp all of them will be passed to the compiler.

g++ -std=c++11 *.cpp
               expands to
g++ -std=c++11 Main.cpp Foo.cpp Bar.cpp

If Main.cpp is the only .cpp file in the directory both your commands will do the same thing.
Last edited on
> multiple definition of
http://www.cplusplus.com/forum/general/140198/


> it's better than running the default ./a.out
¿why?
> it's better than running the default ./a.out
¿why?

Well, you know - so it has it's own name :+) Imagine one has written a bunch of utility programs: they exist in their own development directories; but one may want to collect them together into one directory.

The OP may not have known about this feature, and one day they may want to produce the assembler code, or make a library file.

Regards :+)
Topic archived. No new replies allowed.