Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use g++ to create object file with a specific name

Tags:

I am making a makefile and one of the targets is exptrtest.o how do I use g++ to create an objectfile with that name, the name of my cpp file is exprtest.cpp not exptrtest.cpp?

exptrtest.o: exprtest.cpp     g++ -Wall -g -c exprtest.cpp 

to make it more clear, this is my makefile:

all: exprtest exprtest: exptrtest.o driver.o parser.tab.o scanner.o     g++ -Wall -g -o exprtest exptrtest.o driver.o parser.tab.o scanner.o exptrtest.o: exprtest.cpp     g++ -Wall -g -c exptrtest.o exprtest.cpp driver.o: driver.cpp scanner.hpp driver.hpp     g++ -Wall -g -c driver.cpp parser.tab.o: parser.tab.hpp parser.tab.cpp     bison parser.ypp     g++ -Wall -g -c parser.tab.cpp scanner.o: scanner.cpp scanner.hpp     flex -t scanner.ll > scanner.cpp     g++ -Wall -g -c scanner.cpp clean:     rm parser.tab.hpp parser.tab.cpp scanner.cpp 

I'm getting the error: "g++: error: exptrtest.o: No such file or directory make: * [exprtest] Error 1"

like image 301
Amre Avatar asked Nov 12 '12 03:11

Amre


People also ask

Which G ++ option is used to specify the name of the executable file?

Use file-name as the name of the file produced by g++ (usually, this is an executable file).

What does G ++ o do?

g++ -o target_name file_name: Compiles and links file_name and generates executable target file with target_name (or a. out by default).

How is an object file created?

A computer programmer generates object code with a compiler or assembler. For example, under Linux, the GNU Compiler Collection compiler will generate files with a .o extension which use the ELF format. Compilation on Windows generates files with a . obj extension which use the COFF format.

What is the difference between object file and executable?

The main difference between object file and executable file is that an object file is a file generated after compiling the source code while an executable file is a file generated after linking a set of object files together using a linker. C is a general-purpose, high-level programming language.


1 Answers

Use the -o option in conjunction with -c.

exptrtest.o: exprtest.cpp     g++ -Wall -g -c exprtest.cpp -o exptrtest.o 
like image 188
vladr Avatar answered Sep 19 '22 19:09

vladr