From f1641dd4e4178da0fb6aac2a6a41fe848b11ba51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Thu, 4 Feb 2021 17:05:55 +0100 Subject: [PATCH] Solved linking problem by including C++ standard library '-lstdc++'. Also added a minimal working example of Fortran-C++ interfacing in dir 'mwe'. --- Makefile | 14 ++++++++------ mwe/compile.sh | 1 + mwe/main.f90 | 12 ++++++++++++ mwe/worker.cpp | 7 +++++++ mwe/worker.h | 6 ++++++ 5 files changed, 34 insertions(+), 6 deletions(-) create mode 100755 mwe/compile.sh create mode 100644 mwe/main.f90 create mode 100644 mwe/worker.cpp create mode 100644 mwe/worker.h diff --git a/Makefile b/Makefile index cf37720..cb48a63 100644 --- a/Makefile +++ b/Makefile @@ -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 $@ $^ diff --git a/mwe/compile.sh b/mwe/compile.sh new file mode 100755 index 0000000..72b5010 --- /dev/null +++ b/mwe/compile.sh @@ -0,0 +1 @@ +icpc -c worker.cpp && ifort -c main.f90 && ifort -lstdc++ worker.o main.o -o test diff --git a/mwe/main.f90 b/mwe/main.f90 new file mode 100644 index 0000000..4d9a8e1 --- /dev/null +++ b/mwe/main.f90 @@ -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 diff --git a/mwe/worker.cpp b/mwe/worker.cpp new file mode 100644 index 0000000..1b678b8 --- /dev/null +++ b/mwe/worker.cpp @@ -0,0 +1,7 @@ +#include "worker.h" + +void worker() +{ + std::cout << "Hello, World!" << std::endl; +} + diff --git a/mwe/worker.h b/mwe/worker.h new file mode 100644 index 0000000..70ff209 --- /dev/null +++ b/mwe/worker.h @@ -0,0 +1,6 @@ +#include + +extern "C" +{ + void worker(); +}