微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何编译包含不同语言文件的项目并使其交互?

如何解决如何编译包含不同语言文件的项目并使其交互?

我要使用的语言是C,C ++和Python。

我创建了这个测试程序,上面写着一些生日。

main.cpp

#include <iostream>
#include <string>
#include "announce.h"
extern "C" {
    #include "date.h"
    #include "example.h"
}
int main() {
    title();
    Date f(9,10,1990);
    Date m(2,20,1990);
    announce(m,f);
    //something from python?
    return 0;
}

announce.h /announce.cpp打印两个提供的生日。

example.h / example.c打印标题Birthdays作为输出的第一行。

date.h包含Date结构。

到目前为止,Linux makefile是这样的:

# Compilers
CXX_BUILD = g++ -Wall -std=c++17
C_BUILD = gcc -Wall

# Rules
all: tester
tester: main.o example.o announce.o
    $(CXX_BUILD) -o tester main.o example.o announce.o
main.o: main.cpp 
    $(CXX_BUILD) -c main.cpp
announce.o: announce.cpp
    $(CXX_BUILD) -c announce.cpp
example.o: example.c
    $(C_BUILD) -c example.c
clean:
    rm *.o

我想在main()添加两个用Python编码的函数。这是它们在C ++中的样子:

/// I want to make these two functions in a Python file ///
int years (int y1,int y2) {
    if (y1 < y2) return 1;
    if (y2 < y1) return 2;
    return 0;
}
void older (int x) {
    switch (x) {
        case 0: std::cout << "They were born the same year\n"; break;
        default: std::cout << ((x-1) ? "She's older\n" : "He's older\n");
    }
}
/// calling in main ///
    older(years(m.year,f.year));

如何将Python编码的文件集成到此测试程序中?我的约束是“使用C / C ++编写基本程序,[使用Python编写]脚本”。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。