C++ 如何连接c和c++;模块?

C++ 如何连接c和c++;模块?,c++,c,C++,C,我有两张照片: 模块1:一个C程序,构造两个图 模块2:C++程序,检查由模1、生成的两个图的等价性。 我想将模块1的输出馈送到模块2 Module2仅使用结构,与module1中使用的结构相同,唯一的区别是Module2包含一些函数重载等 传输图形的一种方法是将它们写入文件,然后在module2中重新读取和解析 我的问题是: gcc -c -o module1.o module1.c // C module g++ -c -o module2.o module2.cpp // C++ modu

我有两张照片:

模块1:一个C程序,构造两个图

模块2:C++程序,检查由模1、

生成的两个图的等价性。 我想将模块1的输出馈送到模块2

Module2仅使用结构,与module1中使用的结构相同,唯一的区别是Module2包含一些函数重载等

传输图形的一种方法是将它们写入文件,然后在module2中重新读取和解析

我的问题是:

gcc -c -o module1.o module1.c // C module
g++ -c -o module2.o module2.cpp // C++ module
g++ -o main main.cpp module1.o module2.o // link fine
有没有一种方法可以让我直接将Module1中构造的结构实例携带到Module2,而不必读/写这个文件

我想说的抽象示例:

// Module1: 

struct s1 {
   int a;
};

void main (){
    struct s1 s;
    s.a = 10;
}

// Module2:
struct s1{
    int a;  // Note the same structure and variable name.
};

void print (struct s1 s){
    cout << s.a;
}

void main (){
    struct s1 s;
     print (s);
}
//模块1:
结构s1{
INTA;
};
空干管(){
结构s1s;
s、 a=10;
}
//模块2:
结构s1{
int a;//注意相同的结构和变量名。
};
作废打印(结构s1 s){

cout我认为最简单的解决方案是从这两个库中创建一个库(.dll/.so/),它包含类的实现,并提供一个用于访问它的C和C++接口

然后,您可以将对象的实例存储在库中,并将句柄/副本传递给用户(取决于您的需要),或者在模块之间使用共享内存和传递指针。
在VisualStudio下,共享内存选项称为“多线程DLL(\Md)”,我不知道其他操作系统/编译器的确切选项。

没有理由不能将这两个模块链接到一个程序中。
C
函数声明(在
C
头文件module1.h中)需要标记为具有到
C++
编译器的
C
语言链接

您可以使用
\uuu cplusplus
防护装置执行此操作:

模块1.h

#ifndef MODULE_1_H
#define MODULE_1_H

// tell the C++ compiler this header is for 
// a module written in C
#ifdef __cplusplus
extern "C" {
#endif

typedef struct
{
    int i;
    double d;
} Graph;

Graph construct_a_graph();

#ifdef __cplusplus
} // extern "C"
#endif

#endif // MODULE_1_H
#ifndef MODULE_2_H
#define MODULE_2_H

#include "module1.h"

void process_graph(Graph g);

#endif // MODULE_2_H
模块1.c

#include "module1.h"

Graph construct_a_graph()
{
    Graph g;
    g.i = 2;
    g.d = 7.9;

    return g;
}
模块2.h

#ifndef MODULE_1_H
#define MODULE_1_H

// tell the C++ compiler this header is for 
// a module written in C
#ifdef __cplusplus
extern "C" {
#endif

typedef struct
{
    int i;
    double d;
} Graph;

Graph construct_a_graph();

#ifdef __cplusplus
} // extern "C"
#endif

#endif // MODULE_1_H
#ifndef MODULE_2_H
#define MODULE_2_H

#include "module1.h"

void process_graph(Graph g);

#endif // MODULE_2_H
模块2.cpp

#include "module2.h"

void process_graph(Graph g)
{
    // do stuff in C++
}
#include "module1.h"
#include "module2.h"

int main()
{
    Graph g = construct_a_graph(); // C function

    process_graph(g); // C++ function
}
main.cpp

#include "module2.h"

void process_graph(Graph g)
{
    // do stuff in C++
}
#include "module1.h"
#include "module2.h"

int main()
{
    Graph g = construct_a_graph(); // C function

    process_graph(g); // C++ function
}
使用GCC编译:

gcc -c -o module1.o module1.c // C module
g++ -c -o module2.o module2.cpp // C++ module
g++ -o main main.cpp module1.o module2.o // link fine

正如@Galik所回答的,这两个模块都可以链接到一个程序。作为替代方案,消息传递接口可以用于将一个程序中的信息传递给另一个程序。以下是在您的情况下如何实现的

  • CPP程序
    module2
    生成一个调用
    module1
    的进程
  • module1
    设置结构s1
    s
    成员的值,并将结构作为缓冲区发送到
    module2
  • module2
    接收缓冲区的副本,将其放入结构s1并打印成员的值

这里是C++程序代码<代码>模块2>代码>,由代码> MPICC模块2.CPP-O模块2>代码>:< /P>编译

#include <iostream> 
using namespace std;

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

struct s1{
    int a;  // Note the same structure and variable name.
};

void print (struct s1 s){
    cout << s.a;
}

int main (int argc, char *argv[])
{
    int  errcode;
    MPI_Comm intercomm,intracomm;
    MPI_Info info;
    MPI_Init( &argc, &argv );
    char command[15];
    sprintf(command,"%s","./module1");


    int ierr=MPI_Comm_spawn(command,MPI_ARGV_NULL,1,MPI_INFO_NULL,0,MPI_COMM_WORLD,&intercomm,&errcode);
    if (ierr != MPI_SUCCESS) {
        printf("spaw failed\n");
    }
    // intercomm has two group. One with the parent, one with the children
    MPI_Intercomm_merge(intercomm,0,&intracomm);
    int size, rank;  
    MPI_Comm_size(intracomm, &size);
    MPI_Comm_rank(intracomm, &rank);
    cout<<"parent has rank "<<rank<<" in communicator of size "<<size<<endl;

    struct s1 s;
    MPI_Recv(&s,sizeof(struct s1),MPI_CHAR,1,0,intracomm,MPI_STATUS_IGNORE);
    MPI_Comm_disconnect(&intracomm); //disconnect after all communications
    MPI_Comm_disconnect(&intercomm);

    print (s);
    cout<<endl;
    MPI_Finalize();
    return 0;
}

整件事情都是通过代码> MPRUN -NP 1模块2>代码> < /P>搜索在你使用的任何平台上如何使用共享内存的。顺便说一下,主< <代码>函数,在C语言和C++语言中,都返回一个<代码> int >代码>操作系统。总是。在旧的时候,人们使用文件来共享程序间的数据。你可能需要。创建另一个用作信号量的文件。如果您有源代码并且正在构建单个应用程序,只需调用函数即可。没有理由不能将

C
C++
对象文件链接在一起以生成单个程序。