如何编译(一个源代码中不同包的两个混合C ++源代码) [英] How to compile (a two mixed C++ source codes from different packages in a single source code)
问题描述
说我在两个不同的目录中有两个独立的 .cpp
代码:(请注意,这只是我的问题的示意图)。
这里是第一个...可以在自己的目录中成功编译,它有自己的Makefile
//要包含的特殊库
#includeacado.h
#includeauxiliary_functions.c
/ * ------------ -------------- * /
//为特殊类创建对象
ACADOvariables acadoVariables;
ACADOworkspace acadoWorkspace;
int main(){
//执行任务A_1
//执行任务A_2
//任务A_1和A_2取决于特别包含的标题
return 0;
}
这里是第二个...再次,这段代码可以已成功编译在自己的目录中,它有自己的Makefile
#include< stdio.h>
#include< stdlib.h>
#includeAria.h
/ * -------------------------- * /
/ /为特殊类创建对象
ArPose pose;
ArRobot robot;
int main(){
//执行任务B_1
//执行任务B_2
//任务B_1和B_2取决于特别包含的标题
return 0;
}
现在,为了我的目的,我需要一个源代码。 。
//从两个包中包含的特殊库
#include< stdio.h>
#include< stdlib.h>
#includeAria.h
#includeacado.h
#includeauxiliary_functions.c
/ * ----------- --------------- * /
//从Part1创建特殊类的对象
ACADOvariables acadoVariables;
ACADOworkspace acadoWorkspace;
/ * -------------------------- * /
//从part2 $ b $创建特殊类的对象b ArPose姿势;
ArRobot robot;
int main(){
//执行任务B_1
//执行任务A_1(此任务取决于B_1返回的值)
//执行任务B_2 A_1返回的值
//执行任务A_2(此任务取决于B_1返回的值)
return 0;
}
因此,如何使用这两个包, makefiles,我已经编译了这最后一段代码? ...我试图把两个包内容(文件和文件夹)到一个目录,一个makefile包含单个makefile的内容,但这不是成功编译第三个脚本...
您的帮助非常感谢...
将每个包编译为库。 我错了这两个文件都包含一个 main()
函数,清楚地表明它们都被编译成单独的可执行文件。好吧,你必须考虑你想要重用的代码,并将它们包含在你的项目中,或者从中创建库并链接它们。
关于如何链接库的老回复;在这里需要更多的:
在你自己的包中传递编译器标志:
-
-I
path_to_the_headers 用于编译阶段,所以#include指令找到头。 -
-L
path_to_the _.a
_files 查找链接阶段的库 -
-l </ code> libname ,以告知编译器使用哪些库
<链接阶段的
最后一个选项有点奇怪,如果库被调用 lib
;在具有大多数编译器但不是gcc的Windows上,库将被称为 something .lib
。
Say I have two independent .cpp
codes in two different directories: (please note that this is just a schematic of my question).
Here is the first one ... which can be successfully compiled in its own directory which has its own Makefile
// special libraries to include
#include "acado.h"
#include "auxiliary_functions.c"
/* -------------------------- */
// Create objects for special classes
ACADOvariables acadoVariables;
ACADOworkspace acadoWorkspace;
int main(){
// perform task A_1
// perform task A_2
// Tasks A_1 and A_2 depend on the specially included headers
return 0;
}
And, here is the second one ... Again, this code can be successfully compiled in its own directory which has its own Makefile
#include <stdio.h>
#include <stdlib.h>
#include "Aria.h"
/* -------------------------- */
// Create objects for special classes
ArPose pose;
ArRobot robot;
int main(){
// perform task B_1
// perform task B_2
// Tasks B_1 and B_2 depend on the specially included headers
return 0;
}
Now, for my purposes, I need to have a source code like ...
// special libraries to include from both packages
#include <stdio.h>
#include <stdlib.h>
#include "Aria.h"
#include "acado.h"
#include "auxiliary_functions.c"
/* -------------------------- */
// Create objects for special classes from Part1
ACADOvariables acadoVariables;
ACADOworkspace acadoWorkspace;
/* -------------------------- */
// Create objects for special classes from part2
ArPose pose;
ArRobot robot;
int main(){
// perform task B_1
// perform task A_1 (this task depends on values returned by B_1)
// perform task B_2 (this task depends on values returned by A_1)
// perform task A_2 (this task depends on values returned by B_1)
return 0;
}
So, how can I use the two packages, and the two makefiles that I already have to compile this last piece of code ? ... I tried to put both packages contents (files and folders) into a single directory, with a makefile that contains both contents of the individual makefiles, but this was not successful to compile the third script...
Your help is really appreciated ...
You compile each of the packages to a library. That's probably what the makefiles already do.
Edit: I somehow missed that both those files contain a main()
function, clearly indicating they are each compiled into separate executable. Well, you'll have to factor the code you want to reuse out of them and either include it in your project or create libraries from it and link them in.
Old response about how to link library; much more will be needed here:
Than in your own package you pass the compiler flags:
-I
path_to_the_headers for compilation stage, so the #include directives find the headers.-L
path_to_the_.a
_files for link stage to tell the compiler where to look for the libraries-l
libname for link stage to tell the compiler which libraries to use
The last option is a little strange in that if the library is called lib
something.a
, you write just -l
something; on Windows with most compilers, but not gcc, the library would be called something.lib
instead.
这篇关于如何编译(一个源代码中不同包的两个混合C ++源代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!