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

C++调用C函数

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

1.C代码

#include <stdio.h>

void f(void)
{
    printf("This is a C code\n");
}

2.C++代码

#include <iostream>


using namespace std;


extern "C"{
    void f();
}


void func(void)
{
    cout << "begin used within C++ code" << endl;
}


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

3.编译

#!/bin/bash

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

4.运行结果

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

【上篇】
【下篇】

抱歉!评论已关闭.