简体   繁体   中英

Running make for a C++ project under Eclipse

I am developing a C++ application under Ubuntu 10.10, using g++ and automake. For this program I have two cpp files (main.cpp and forward.cpp, the latter one is a test class), and forward.h; I also have the following makefile:

main: \
forward.o
    g++ -fPIC -g -Wall -D_REENTRANT  -fno-exceptions  -I/usr/local/Aria/include/ -L/usr/local/Aria/lib -lAria -lpthread -ldl -lrt \
    -o simple_controller \
    main.cpp \
    forward.o

%.o : %.cpp
    g++ -c -g -Wall -D_REENTRANT -fno-exceptions  -I/usr/local/Aria/include/ $< -o $@

clean:
    rm -fv *.o
    rm -fv */*.o

When I copy all those four files into the same directory and call "make" from command line (bash) then g++ is called and my program will be compiled correctly.

Ok, now I wanted to achieve the same under Eclipse. So I created a new unmanaged C++ project under Eclipse, so that I can provide my own makefile (the same as listed above). Unfortunately, when I now use the "Build all" option under Eclipse I can see the following console output:

make 
make: *** No rule to make target `forward.o', needed by `main'.  Stop.

Since I am new to C++ development under Linux, using g++ and makefiles, I do not really understand the problem. I thought that everything that is needed to compile my application correctly is written in the makefile, as it works perfectly from the command line. But it seems I cannot copy the same makefile 1:1 into my Eclipse project.

Any ideas what I am missing here?
Thanks in advance for your help!

when you do a build of the project eclipse is executing a make all command, so you will either need to modify your project to use main instead of all for the target eclipse uses, or add an all rule to your makefile that references back up to the main rule. Also I would add a command under the clean step to remove the executable as well.

Modified Makefile:

main: \
    forward.o
    g++ -fPIC -g -Wall -D_REENTRANT  -fno-exceptions  -I/usr/local/Aria/include/ -       L/usr/local/Aria/lib -lAria -lpthread -ldl -lrt \
    -o simple_controller \
    main.cpp \
    forward.o

%.o : %.cpp
    g++ -c -g -Wall -D_REENTRANT -fno-exceptions  -I/usr/local/Aria/include/ $< -o $@

clean:
    rm -fv *.o
    rm -fv */*.o
    rm -fv simple_controller

all: main

Ok, I found the problem now -- Eclipse is putting the makefile into another directory than the "src" folder where all source code files are located. So make did not find the .cpp and .h files needed for compilation. Now I ended up in something like this:

...
forward.o : ../src/forward.cpp ../src/forward.h
...

Is there any way to tell make that it should also look into the folder ../src even when not explicitely stated?

You can tell make to change directories with the -C option.

For example if your source is in src/ and your makefile is in the parent directory of src/ you can simply do:

make -C ..

Note - Never used Eclipse specifically but usually there is a way to add commandline switches & options to your build command somewhere in the IDE options.

(Answer based on the second part of your question where you specify that the problem is the makefile being in a different folder then your source.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM