简体   繁体   English

在Eclipse CDT中包含来自另一个C ++项目的源文件

[英]Including source files from another C++ project in Eclipse CDT

I have a C++ project in Eclipse CDT which uses some functions from OpevCV and it compiles into an executable and works fine. 我在Eclipse CDT中有一个C ++项目,它使用了一些来自OpevCV的函数,它编译成一个可执行文件,工作正常。 Now I want to have a new C++ project which uses some of the functions defined in the first one. 现在我想要一个新的C ++项目,该项目使用第一个中定义的一些函数。 I can't get the two to compile and link together. 我无法将两者编译并链接在一起。

I have #include "..." in the relevant files in the new project, I've added the old project to the referenced projects of the new project and in the build settings of the new project, I've added the source folder of the old project as an include ( -I gcc option). 我在新项目的相关文件中有#include "..." ,我已将旧项目添加到新项目的引用项目中,并且在新项目的构建设置中,我添加了源文件夹将旧项目作为include( -I gcc选项)。

I've attached a screenshot of my Eclipse in case it's helpful: 我附上了Eclipse的截图,以防它有用:

在此输入图像描述

The error given by the compiler is: 编译器给出的错误是:

Building target: OtherProject 构建目标:OtherProject
Invoking: GCC C++ Linker 调用:GCC C ++链接器
g++ -o "OtherProject" ./src/foo.o g ++ -o“OtherProject”./ src / foo.o
./src/foo.o: In function `main': ./src/foo.o:在函数`main'中:
/home/max/opencvjni/OtherProject/Debug/../src/foo.cpp:13: undefined reference to threshold()' /home/max/opencvjni/OtherProject/Debug/../src/foo.cpp:13:对阈值()的未定义引用
collect2: ld returned 1 exit status collect2:ld返回1退出状态
make: *** [OtherProject] Error 1 make:*** [OtherProject]错误1

Here is the code in those other files in the other tabs of the screenshot: 以下是屏幕截图其他选项卡中其他文件中的代码:

Threshold.cpp Threshold.cpp

#include <cv.h>
#include <highgui.h>
#include "Threshold.h"

using namespace cv;

int threshold(void) {
   Mat img = imread("/home/max/opencvjni/monalisa.jpg", 0);
   threshold(img, img, 127, 255, THRESH_BINARY);
   imwrite("/home/max/opencvjni/monathresh.jpg", img);
   return 0;
}

int main(void) {
  threshold();
  return 0;
}

Threshold.h Threshold.h

#ifndef THRESHOLD_H_
#define THRESHOLD_H_

int threshold(void);

#endif /* THRESHOLD_H_ */[/code]
  • Eclipse version: 3.7.2 Eclipse版本:3.7.2
  • CDT version: 8.0.2.201202111925 CDT版本:8.0.2.201202111925
  • OS: Ubuntu 12.04 操作系统:Ubuntu 12.04

Thank you for looking at my post, if there is no way to do this, or it is just more sensible to do it another way, please don't hesitate to say so. 感谢您查看我的帖子,如果没有办法做到这一点,或者以另一种方式做事更为明智,请不要犹豫,这样说。

Regards 问候

I was looking for an answer too but the previous answers did not solve the problem. 我也在寻找答案,但之前的答案并没有解决问题。 (Nick's answer was helpful). (尼克的回答很有帮助)。 You have to define 2 things: 你必须定义两件事:

  1. Go to Project->Properties->C/C++ general->Paths and Symbols . 转到Project->Properties->C/C++ general->Paths and Symbols Select the Source Location tab and click the Link Folder... , Then select the folder you want to include. 选择“ Source Location选项卡,然后单击“ Link Folder... ,然后选择要包括的文件夹。
  2. Go to Project->Properties->C/C++ general->Paths and Symbols . 转到Project->Properties->C/C++ general->Paths and Symbols Select the Includes tab. 选择Includes选项卡。 Make sure you select the correct language on the left menu (for example: GNU C++ ) and click Add... , Then select the folder you want to include. 确保在左侧菜单中选择了正确的语言(例如: GNU C++ )并单击Add... ,然后选择要包含的文件夹。

Note: I'm not sure it's necessary but when I created the external project I selected the "Shared Library" option in the new project wizard. 注意:我不确定是否有必要,但在创建外部项目时,我在新项目向导中选择了“共享库”选项。

To make sure you did everything OK, go to : Project->Properties->C/C++ Build->Settings . 要确保一切正常,请转到: Project->Properties->C/C++ Build->Settings Select the GCC C++ Linker and make sure that the All options contains "-L/your_folder_name". 选择GCC C++ Linker并确保All options包含“-L / your_folder_name”。 Repeat this for GCC C++ Compiler also. GCC C++ Compiler也重复此操作。

Go to Project->Properties->C/C++ general->Paths and Symbols. 转到项目 - >属性 - > C / C ++ general->路径和符号。 Select the 'Source Location' tab and click the 'Link Folder...' button. 选择“源位置”选项卡,然后单击“链接文件夹...”按钮。 You should be able to select the directory containing the project you want to reference. 您应该能够选择包含要引用的项目的目录。 All the source files for that project will be included in your current project. 该项目的所有源文件都将包含在当前项目中。 Works for me anyway. 无论如何,对我有用。

As a new user I could not comment on Itay Gal's answer, so this is a really klunky way to add to that answer. 作为一个新用户,我无法评论Itay Gal的答案,所以这是一个非常笨重的方式来添加答案。 My additions may be stating the obvious, but it was not all that obvious to me. 我的补充可能是明显的,但对我来说并不是那么明显。

From Itay Gal You have to define 2 things: 从Itay Gal你必须定义两件事:

from the referring project 来自推荐项目

1. Go to Project->Properties->C/C++ general->Paths and Symbols. 1.转到项目 - >属性 - > C / C ++ general->路径和符号。 Select the Source Location tab and click the Link Folder..., Then select the folder you want to include. 选择“源位置”选项卡,然后单击“链接文件夹...”,然后选择要包括的文件夹。

To "select the folder you want to include", I used Link Folder->Advanced , clicked the "Link to Folder in the file system" check-box and browsed to my folder's location, which is in my workspace location. 要“选择要包含的文件夹”,我使用了“ 链接文件夹 - >高级” ,单击“文件系统中链接到文件夹”复选框,然后浏览到我文件夹的位置,该位置位于我的工作区位置。 The "folder name" is the sub directory of my referring project in which my referenced files will appear and was filled in automatically when I selected my referenced folder. “文件夹名称”是我的引用项目的子目录,其中我的引用文件将出现并在我选择引用的文件夹时自动填充。 You can edit the folder name, but you cannot give it a path so as to make it a sub-directory. 您可以编辑文件夹名称,但不能为其指定路径以使其成为子目录。

2. Go to Project->Properties->C/C++ general->Paths and Symbols. 2.转到项目 - >属性 - > C / C ++ general->路径和符号。 Select the Includes tab. 选择包含选项卡。 Make sure you select the correct language on the left menu (for example: GNU C++) and click Add..., Then select the folder you want to include. 确保在左侧菜单中选择了正确的语言(例如:GNU C ++)并单击添加...,然后选择要包含的文件夹。

Expanding upon "select the folder you want to include", from the "includes" tab, I clicked Add->Workspace , selected the referenced project and browsed to the referenced folder and clicked OK 扩展“选择要包含的文件夹”,在“包含”选项卡中,单击“ 添加” - >“工作区” ,选择引用的项目并浏览到引用的文件夹,然后单击“确定”。

I did not make my external project as a library, just a few files kept in another git repository which do not even build by themselves. 我没有把我的外部项目作为一个库,只有一些文件存放在另一个git存储库中,甚至没有自己构建。 I wanted them separate because the code is used in common by several other projects. 我希望它们分开,因为代码被其他几个项目共同使用。

To make sure you did everything OK, go to : Project->Properties->C/C++ Build->Settings. 要确保一切正常,请转到:项目 - >属性 - > C / C ++构建 - >设置。 Select the GCC C++ Linker and make sure that the All options contains "-L/your_folder_name". 选择GCC C ++链接器并确保所有选项包含“-L / your_folder_name”。 Repeat this for GCC C++ Compiler also. 对GCC C ++编译器也重复此操作。

I found my linked project under Project->Properties->C/C++Build->Settings->Cross ARM C++ Compiler in the "all options" window, but not in the ->Cross ARM C++ Linker tab "all options" window. 我在“所有选项”窗口的项目- >属性 - > C / C ++构建 - >设置 - >跨ARM C ++编译器下找到了我的链接项目,但未在- >跨ARM C ++链接器选项卡中找到“所有选项”窗口。 My code seems to build properly and properly launch a debug window anyway. 我的代码似乎正确构建并正确启动调试窗口。

Many thanks to Itay gal for the answer. 非常感谢Itay gal的答案。

So it worked great for my first project, as reported above. 因此,如上所述,它对我的​​第一个项目非常有用。 Then I tried to do the same thing with two other projects which needed to use the referenced code. 然后我尝试用另外两个需要使用引用代码的项目做同样的事情。 After spending a long bit of time, I concluded that the project includes were different in the "Sources" directory than they were in the overall project. 花了很长时间后,我得出结论,“源”目录中的项目包含与在整个项目中的不同。 If you select the project and apply step 2 you get a different result than if you select the "Source" directory and apply step 2. This might be something I managed to inadvertently turn on in floundering around learning about eclipse, but if it is I see nowhere to turn it off. 如果您选择项目并应用第2步,则会得到与您选择“源”目录并应用第2步不同的结果。这可能是我无意中开始学习有关eclipse的问题,但如果是我无处可去。

I personally like CMake to specify my build options. 我个人喜欢CMake来指定我的构建选项。 CMake projects can also be imported into Eclipse CDT. CMake项目也可以导入Eclipse CDT。 CMake allows you to generate make file projects on linux, but also VS projects on windows. CMake允许您在Linux上生成make文件项目,还可以在Windows上生成VS项目。 The OpenCV website also gives an example on how to use it with CMake. OpenCV网站还举例说明了如何在CMake中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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