在 Eclipse CDT 中包含来自另一个 C++ 项目的源文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13240893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including source files from another C++ project in Eclipse CDT
提问by Max Spencer
I have a C++ project in Eclipse CDT which uses some functions from OpevCV and it compiles into an executable and works fine. Now I want to have a new C++ project which uses some of the functions defined in the first one. I can't get the two to compile and link together.
我在 Eclipse CDT 中有一个 C++ 项目,它使用 OpevCV 中的一些函数,它编译成一个可执行文件并且工作正常。现在我想要一个新的 C++ 项目,它使用第一个中定义的一些函数。我无法让两者编译和链接在一起。
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 "..."
在新项目的相关文件中,将旧项目添加到新项目的引用项目中,在新项目的构建设置中,将旧项目的源文件夹添加为包含(-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
Invoking: GCC C++ Linker
g++ -o "OtherProject" ./src/foo.o
./src/foo.o: In function `main':
/home/max/opencvjni/OtherProject/Debug/../src/foo.cpp:13: undefined reference to threshold()'
collect2: ld returned 1 exit status
make: *** [OtherProject] Error 1
构建目标:OtherProject
调用:GCC C ++链接器
克++ -o “OtherProject” ./src/foo.o
./src/foo.o:在函数'主:
/home/max/opencvjni/OtherProject/Debug/../ src/foo.cpp:13: 未定义引用阈值 ()'
collect2: ld 返回 1 退出状态
make: *** [OtherProject] 错误 1
Here is the code in those other files in the other tabs of the screenshot:
这是屏幕截图其他选项卡中其他文件中的代码:
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
阈值.h
#ifndef THRESHOLD_H_
#define THRESHOLD_H_
int threshold(void);
#endif /* THRESHOLD_H_ */[/code]
- Eclipse version: 3.7.2
- CDT version: 8.0.2.201202111925
- OS: Ubuntu 12.04
- Eclipse 版本:3.7.2
- CDT 版本:8.0.2.201202111925
- 操作系统: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
问候
回答by Itay Gal
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:
我也在寻找答案,但之前的答案并没有解决问题。(尼克的回答很有帮助)。你必须定义两件事:
- Go to
Project->Properties->C/C++ general->Paths and Symbols
. Select theSource Location
tab and click theLink Folder...
, Then select the folder you want to include. - Go to
Project->Properties->C/C++ general->Paths and Symbols
. Select theIncludes
tab. Make sure you select the correct language on the left menu (for example:GNU C++
) and clickAdd...
, Then select the folder you want to include.
- 去
Project->Properties->C/C++ general->Paths and Symbols
。选择Source Location
选项卡并单击Link Folder...
,然后选择要包含的文件夹。 - 去
Project->Properties->C/C++ general->Paths and Symbols
。选择Includes
选项卡。确保在左侧菜单中选择正确的语言(例如:)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
. Select the GCC C++ Linker
and make sure that the All options
contains "-L/your_folder_name".
Repeat this for GCC C++ Compiler
also.
为确保一切顺利,请转至 : Project->Properties->C/C++ Build->Settings
。选择GCC C++ Linker
并确保All options
包含“-L/your_folder_name”。也重复此操作GCC C++ Compiler
。
回答by Nick Brett
Go to Project->Properties->C/C++ general->Paths and Symbols. 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.
转到项目-> 属性-> C/C++ 常规-> 路径和符号。选择“源位置”选项卡,然后单击“链接文件夹...”按钮。您应该能够选择包含要引用的项目的目录。该项目的所有源文件都将包含在您当前的项目中。反正对我有用。
回答by Andrew Meyer
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. My additions may be stating the obvious, but it was not all that obvious to me.
作为一个新用户,我无法对 Itay Gal 的回答发表评论,所以这是添加到该答案中的一种非常笨拙的方式。我的补充可能是显而易见的,但对我来说并不是那么明显。
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. Select the Source Location tab and click the Link Folder..., Then select the folder you want to include.
1. 转到项目->属性->C/C++ 通用->路径和符号。选择“源位置”选项卡并单击“链接文件夹...”,然后选择要包含的文件夹。
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.
要“选择要包含的文件夹”,我使用了Link Folder->Advanced,单击“链接到文件系统中的文件夹”复选框并浏览到我的文件夹位置,该位置位于我的工作区位置。在“文件夹名称”是我在我的引用文件,将出现,并装入时自动我选我所引用的文件夹指项目的子目录。您可以编辑文件夹名称,但不能为其指定路径以使其成为子目录。
2. Go to Project->Properties->C/C++ general->Paths and Symbols. 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.
2. 转到项目->属性->C/C++ 通用->路径和符号。选择包括选项卡。确保在左侧菜单中选择正确的语言(例如: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
展开“选择要包含的文件夹”,从“包含”选项卡中,单击Add->Workspace,选择引用的项目并浏览到引用的文件夹并单击 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. I wanted them separate because the code is used in common by several other projects.
我没有将我的外部项目作为一个库,只是保存在另一个 git 存储库中的几个文件,这些文件甚至不由自己构建。我希望它们分开,因为代码被其他几个项目共同使用。
To make sure you did everything OK, go to : Project->Properties->C/C++ Build->Settings. Select the GCC C++ Linker and make sure that the All options contains "-L/your_folder_name". Repeat this for GCC C++ Compiler also.
为确保一切正常,请转到:项目->属性->C/C++ 构建->设置。选择 GCC C++ 链接器并确保所有选项包含“-L/your_folder_name”。也对 GCC C++ 编译器重复此操作。
I found my linked project under Project->Properties->C/C++Build->Settings->Cross ARM C++ Compilerin the "all options"window, but not in the ->Cross ARM C++ Linkertab "all options"window. My code seems to build properly and properly launch a debug window anyway.
我在“所有选项”窗口中的Project->Properties->C/C++Build->Settings->Cross ARM C++ Compiler下找到了我的链接项目,但未在->Cross ARM C++ Linker选项卡“所有选项”中找到我的链接项目窗户。我的代码似乎正确构建并正确启动调试窗口。
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.
所以它对我的第一个项目很有用,如上所述。然后我尝试对其他两个需要使用引用代码的项目做同样的事情。花了很长时间后,我得出结论,“Sources”目录中的项目包含与整个项目中的不同。如果您选择项目并应用第 2 步,您会得到与选择“源”目录并应用第 2 步的结果不同的结果。这可能是我在学习 eclipse 的过程中无意中打开的,但如果是我无处可关。
回答by gvd
I personally like CMake to specify my build options. CMake projects can also be imported into Eclipse CDT. CMake allows you to generate make file projects on linux, but also VS projects on windows. The OpenCV website also gives an exampleon how to use it with CMake.
我个人喜欢 CMake 来指定我的构建选项。CMake 项目也可以导入 Eclipse CDT。CMake 允许你在 linux 上生成 make 文件项目,也可以在 windows 上生成 VS 项目。OpenCV 网站还提供了有关如何将其与 CMake 一起使用的示例。