为 C++ 运行 make Eclipse下的项目
我正在 Ubuntu 10.10 下使用 g++ 和 automake 开发 C++ 应用程序。对于这个程序我有两个cpp文件(main.cpp和forward.cpp,后一个是测试类)和forward.h;我还有以下 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
当我将所有这四个文件复制到同一目录并从命令行(bash)调用“make”时,将调用 g++ 并且我的程序将被正确编译。
好吧,现在我想在 Eclipse 下实现同样的目标。因此,我在 Eclipse 下创建了一个新的非托管 C++ 项目,以便我可以提供自己的 makefile(与上面列出的相同)。不幸的是,当我现在在 Eclipse 下使用“Build all”选项时,我可以看到以下控制台输出:
make
make: *** No rule to make target `forward.o', needed by `main'. Stop.
由于我是 Linux 下 C++ 开发的新手,使用 g++ 和 makefile,我并不真正理解这个问题。我认为正确编译应用程序所需的所有内容都写在 makefile 中,因为它可以在命令行中完美运行。但似乎我无法将相同的 makefile 1:1 复制到我的 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/ lt; -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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您构建项目时,Eclipse 正在执行 make all 命令,因此您需要修改项目以使用 main 而不是 all 来满足 Eclipse 使用的目标,或者向 makefile 添加一条引用备份的 all 规则主要规则。另外,我会在 clean 步骤下添加一个命令来删除可执行文件。
修改后的Makefile:
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:
好吧,我现在发现了问题——Eclipse 将 makefile 放入了所有源代码文件所在的“src”文件夹之外的另一个目录中。所以make没有找到编译所需的.cpp和.h文件。现在我最终得到了这样的结果:
有没有办法告诉 make 即使没有明确说明,它也应该查看文件夹 ../src ?
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:
Is there any way to tell make that it should also look into the folder ../src even when not explicitely stated?
您可以使用 -C 选项告诉 make 更改目录。
例如,如果您的源代码位于 src/ 中,并且 makefile 位于 src/ 的父目录中,您可以简单地执行以下操作:
注意 - 从未专门使用过 Eclipse,但通常有一种方法可以添加命令行开关和命令。 IDE 选项中某处的构建命令的选项。
(根据问题的第二部分回答,您指定问题是 makefile 位于与源不同的文件夹中。)
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:
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.)