I'm having problem compiling cpp file with C inline functions using g++11. Following is an example:
c.h:
#ifndef C_H
#define C_H
#ifdef __cplusplus
extern "C" {
#endif
inline int add(int a,int b){return a+b;}
#ifdef __cplusplus
}
#endif
#endif
c.c
#include "c.h"
extern inline int add(int a,int b);
cpp.cpp:
#include "c.h"
int main(){
return add(1,2);
}
Compile cmd
gcc -c c.c
g++ -c cpp.cpp
g++ c.o cpp.o
Problem: Link error. Multiple definitions of add.
The situation is using a C++ test framework to test a C code base. So I cannot modify the C code.
Platform is MSYS2/MinGW64, gcc 11.2.0. nm cpp.o
gives the following:
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 p .pdata
0000000000000000 p .pdata$add
0000000000000000 r .rdata$zzz
0000000000000000 t .text
0000000000000000 t .text$add
0000000000000000 r .xdata
0000000000000000 r .xdata$add
U __main
0000000000000000 T add
0000000000000000 T main