当出现以下编译异常,并且排查库存在,代码也定义,但无论如何链接都出现异常时,需要注意extern 'C’的问题
src/main.o:在函数‘main’中:
main.cpp:62:对‘avformat_alloc_context()’未定义的引用
1.cpp文件中的内容
#include "mytest.h"
#include <iostream>
using namespace std;
int add(const int a, const int b)
{
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return (a + b);
}
2.hpp文件中的内容
在函数声明时添加extern “C”,就可以在C中调用。为了同时可以在C++中调用,可以写成下面这样。
#ifdef __cplusplus
extern "C"
{
#endif
int add(const int a, const int b);
#ifdef __cplusplus
}
#endif
3.c文件中的内容
#include <stdio.h>
#include <stdlib.h>
#include "mytest.h"
void main() {
printf("%d\n", add(1, 2));
}
4.运行结果