Solved linking problem by including C++ standard library '-lstdc++'. Also added a minimal working example of Fortran-C++ interfacing in dir 'mwe'.

This commit is contained in:
François Coppens 2021-02-04 17:05:55 +01:00
parent 84fffdb7fa
commit f1641dd4e4
5 changed files with 34 additions and 6 deletions

View File

@ -1,8 +1,8 @@
CXX=icpc
CXXFLAGS=-O0 -debug full -traceback
FC=ifort
FFLAGS=-O0 -debug full -traceback
# ARCH=-xCORE-AVX2
CXX = icpc
CXXFLAGS = -O0 -debug full -traceback
FC = ifort
FFLAGS = -O0 -debug full -traceback
# ARCH = -xCORE-AVX2
## Deps & objs for the C++ stuff
cppDEPS = cppmain.cpp SM_MaponiA3.cpp SM_MaponiA3.hpp Helpers.hpp
@ -11,6 +11,8 @@ cppOBJ = cppmain.o SM_MaponiA3.o
## Deps & objs for the Fortran stuff
fDEPS = fmain.f90 SM_MaponiA3_mod.f90
fOBJ = SM_MaponiA3.o SM_MaponiA3_mod.o fmain.o
fLIBS = -lstdc++
## Compile recipes for C++ stuff
%.o: %.cpp $(cppDEPS)
@ -36,4 +38,4 @@ cppSherman-Morrison: $(cppOBJ)
## Linking Fortran example program calling the C++ function 'Sherman_Morrison()'
fSherman-Morrison: $(fOBJ)
$(FC) $(ARCH) $(FFLAGS) -o $@ $^
$(FC) $(ARCH) $(FFLAGS) $(fLIBS) -o $@ $^

1
mwe/compile.sh Executable file
View File

@ -0,0 +1 @@
icpc -c worker.cpp && ifort -c main.f90 && ifort -lstdc++ worker.o main.o -o test

12
mwe/main.f90 Normal file
View File

@ -0,0 +1,12 @@
program test
use iso_c_binding
implicit none
interface
subroutine hello() bind(C, name="worker")
end subroutine
end interface
call hello()
end program test

7
mwe/worker.cpp Normal file
View File

@ -0,0 +1,7 @@
#include "worker.h"
void worker()
{
std::cout << "Hello, World!" << std::endl;
}

6
mwe/worker.h Normal file
View File

@ -0,0 +1,6 @@
#include <iostream>
extern "C"
{
void worker();
}