现在的位置: 首页 > 综合 > 正文

C调用C++函数

2018年01月11日 ⁄ 综合 ⁄ 共 551字 ⁄ 字号 评论关闭

1.C++代码

#include <iostream>

using namespace std;

extern "C" void func(void)
{
    cout << "This is a C++ code" << endl;
}

2.C代码

#include <stdio.h>

extern void func(void);

void f(void)
{
    printf("being used within C code\n");
}

int main(int argc, char *argv[])
{
    func();
    f();
    return 0;
}

3.编译

#!/bin/bash

g++ -c -Wall -Werror -fPIC cpp_source.cpp
g++ -shared -o libcpp_source.so cpp_source.o
export LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATH
gcc -L/home/csdn/workspace/test -Wall main.c -o main -lcpp_source

4.运行

export LD_LIBRARY_PATH=/home/csdn/workspace/test:$LD_LIBRARY_PATH
This is a C++ code
being used within C code

抱歉!评论已关闭.