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

44,821

Use the -o option in conjunction with -c.

exptrtest.o: exprtest.cpp
    g++ -Wall -g -c exprtest.cpp -o exptrtest.o
Share:
44,821

Related videos on Youtube

Amre
Author by

Amre

Updated on November 12, 2020

Comments

  • Amre
    Amre over 3 years

    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"

    • Trevor Hickey
      Trevor Hickey over 11 years
      "g++ -c name.cpp" will create name.o. Use the -o flag to create a different output name.