How to compile (a two mixed C++ source codes from different packages in a single source code)

北慕城南 提交于 2019-12-24 11:43:28

问题


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 ...


回答1:


In your current scenario, the first thing to check for is whether your makefiles are creating executables rather than simply object files. It seems like the latter if you're saying that the output from each makefile can be run on it's own. If the output is indeed an executable rather than an object file, you're not going to be able to "link" those executables together into a new executable. So what you want to make sure is that the output of the compilation process for each .cpp file is an object file with a .o extension, and that only a single .cpp file has a main() function. The process of creating object files rather than executables can be done using the -c option with g++.

Once you have object and/or library files created from each of the .cpp files, and a single .cpp file with a main() entry-point function, you can then link the object/library files together to create a final executable using g++ that will then include the functionality described in each of the individual .cpp files.




回答2:


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:

  • -Ipath_to_the_headers for compilation stage, so the #include directives find the headers.
  • -Lpath_to_the_.a_files for link stage to tell the compiler where to look for the libraries
  • -llibname 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 libsomething.a, you write just -lsomething; on Windows with most compilers, but not gcc, the library would be called something.lib instead.



来源:https://stackoverflow.com/questions/14814950/how-to-compile-a-two-mixed-c-source-codes-from-different-packages-in-a-single

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!