混合使用 C、C++ 和 Fortran 代码
Mixing code in C, C++, and Fortran
我一直在研究 C、C++ 和 Fortran 的混合代码。我进行的一个简单测试涉及 C++ 中的主程序 (cppprogram.C
):
#include <iostream>
using namespace std;
extern "C" {
void ffunction_(float *a, float *b);
}
extern "C" {
void cfunction(float *a, float *b);
}
void cppfunction(float *a, float *b);
int main() {
float a=1.0, b=2.0;
cout << "Before running Fortran function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
ffunction_(&a,&b);
cout << "After running Fortran function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "Before running C function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cfunction(&a,&b);
cout << "After running C function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "Before running C++ function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cppfunction(&a,&b);
cout << "After running C++ function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return 0;
}
...在 C、C++ 和 Fortran 中调用过程:
C (cfunction1.c
)
void cfunction(float *a, float *b) {
*a=7.0;
*b=8.0;
}
C++ (cppfunction1.C
)
extern "C" {
void cppfunction(float *a, float *b);
}
void cppfunction(float *a, float *b) {
*a=5.0;
*b=6.0;
}
Fortran (ffunction.f
)
subroutine ffunction(a,b)
a=3.0
b=4.0
end
以下是我用来制作目标文件的命令,link它们在一起:
g++ -c cppprogram.C
gcc -c cfunction1.c
g++ -c cppfunction1.C
gfortran -c ffunction.f
g++ -o cppprogram cppprogram.o cfunction1.o cppfunction1.o ffunction.o
这是我的错误:
cppprogram.o: In function `main':
cppprogram.C:(.text+0x339): undefined reference to `cppfunction(float*, float*)'
collect2: error: ld returned 1 exit status
我知道内部编译器有时希望在文件名后附加下划线,但我想我已经处理好了。这可以使用 nm
命令确定。某处有一个小错误...有人看到了吗?非常感谢。
更新:
您在 cppfunction1.C
中将 cppfunction
声明为 extern "C"
,但在 cppprogram.C
中您未将其声明为 extern "C"
。由于 main
是 C++
你不需要在 cppfunction1.C
中将 cppfunction
声明为 extern "C"
除非你希望能够从 C 或 Fortran 中调用它。
从 cppfunction1.C
中删除 extern "C"
。
我一直在研究 C、C++ 和 Fortran 的混合代码。我进行的一个简单测试涉及 C++ 中的主程序 (cppprogram.C
):
#include <iostream>
using namespace std;
extern "C" {
void ffunction_(float *a, float *b);
}
extern "C" {
void cfunction(float *a, float *b);
}
void cppfunction(float *a, float *b);
int main() {
float a=1.0, b=2.0;
cout << "Before running Fortran function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
ffunction_(&a,&b);
cout << "After running Fortran function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "Before running C function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cfunction(&a,&b);
cout << "After running C function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "Before running C++ function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cppfunction(&a,&b);
cout << "After running C++ function:" << endl;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
return 0;
}
...在 C、C++ 和 Fortran 中调用过程:
C (cfunction1.c
)
void cfunction(float *a, float *b) {
*a=7.0;
*b=8.0;
}
C++ (cppfunction1.C
)
extern "C" {
void cppfunction(float *a, float *b);
}
void cppfunction(float *a, float *b) {
*a=5.0;
*b=6.0;
}
Fortran (ffunction.f
)
subroutine ffunction(a,b)
a=3.0
b=4.0
end
以下是我用来制作目标文件的命令,link它们在一起:
g++ -c cppprogram.C
gcc -c cfunction1.c
g++ -c cppfunction1.C
gfortran -c ffunction.f
g++ -o cppprogram cppprogram.o cfunction1.o cppfunction1.o ffunction.o
这是我的错误:
cppprogram.o: In function `main':
cppprogram.C:(.text+0x339): undefined reference to `cppfunction(float*, float*)'
collect2: error: ld returned 1 exit status
我知道内部编译器有时希望在文件名后附加下划线,但我想我已经处理好了。这可以使用 nm
命令确定。某处有一个小错误...有人看到了吗?非常感谢。
更新:
您在 cppfunction1.C
中将 cppfunction
声明为 extern "C"
,但在 cppprogram.C
中您未将其声明为 extern "C"
。由于 main
是 C++
你不需要在 cppfunction1.C
中将 cppfunction
声明为 extern "C"
除非你希望能够从 C 或 Fortran 中调用它。
从 cppfunction1.C
中删除 extern "C"
。