Debugger
Debugger

Reputation: 9488

Getting error while linking with g++

I try to compile and link my application in 2 steps :

Compiling:

g++ -c -o file1.o file1.cc general_header.h
g++ -c -o file2.o file2.cc general_header.h
g++ -c -o file3.o file3.cc general_header.h

Linking:

g++ -o myApp file1.o file2.o file3.o

I'm getting a link error as following:

file1.o: file not recognized: File format not recognized
collect2: ld returned 1 exit status

Am i doing something wrong ?

Thanks

Upvotes: 2

Views: 473

Answers (2)

yassin
yassin

Reputation: 6707

No need to include header files in your input files list

g++ -c -o file1.o file1.cc

Upvotes: 2

anon
anon

Reputation:

You should not be mentioning your header file on the command line - you don't want to compile it directly, but only as it is included in your source files. also, I would let the compiler name the object files, as it's too easy to make a typo when doing this explicitly. So your compilation commands should look like:

g++ -c file1.cc 

and you can then also say:

g++ -c file1.cc file2.cc file3.cc

Upvotes: 3

Related Questions