Combining FORTRAN and C++, linking error - c++

I should couple in linux one c++ code with old fortran code, where fortan is the main code. Im not expert in this area and I try to start with simple test, but still I cannot compile it. Maybe I'm stupid, but I cannot find a working example anywhere. I managed to compile fortran and c, when the linking can be done by ifort (need to use intel compiler later with the actual fortran code). But If I've understood right, with c++, the linking must be done by c++ compiler (g++).
So what do I do wrong here:
My FORTRAN test code "ftest.f":
PROGRAM MAIN
IMPLICIT NONE
INTEGER I
write(*,*) "hello fortran1"
CALL ctest()
write(*,*) "hello fortran2"
END PROGRAM
And C++ code "ctest.cpp"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
extern "C" void ctest_();
void ctest_(){
int i;
// std::cout << "hello c \n";
printf("hello c\n");
}
I try to compile with the following:
ifort -c ftest.f
g++ -c ctest.cpp
g++ -ldl -lm -limf -L -l -lifcore ctest.o ftest.o
And I get an error:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
So what should I do to success with linking this program?

Your main (entry) is in Fortran part, so one way to solve it is to to use ifort linker instead of g++ (that would also link ifcore automatically)
ifort ctest.o ftest.o ... -lstdc++

So looks like I truested too much on one page telling me that I have to use c++ compiler for linking. Earlier just always something else was wrong when trying to link by ifort.
So using ifort with -lstdc++ is really enough with the current version of my test code. Earlier just something else was wrong.
Thank you very much once again, I wish you all the best for your own projects!

Related

CS50 - C++: issue with " using namespace std;" [duplicate]

There's a C++ code:
#include <stdio.h>
int main() {
int b = sizeof('a');
if(b==4) printf("I'm a C program!\n");
else printf("I'm a C++ program!\n");
}
Compile it like this:
gcc main.cpp -o main
It succeeds and gives:
I'm a C++ program!
Then add a line somewhere inside function main
int *p1 = new int [1000];
It fails with:
C:\Users\...\AppData\Local\Temp\cccJZ8kN.o:main1.cpp:(.text+0x1f): undefined reference to operator new[](unsigned long long)'
collect2.exe: error: ld returned 1 exit status
Then the following two commands successfully compile the code:
gcc main.cpp -o main -lstdc++
and
g++ main.cpp -o main
The compiler is minGW-win64 (http://mingw-w64.sourceforge.net/).
The questions are:
Which of the two last commands are better?
To my mind gcc correctly chooses the right compiler but then uses a wrong linker. Is it right?
May it be a problem in minGW-win64?
As I see (correct me if it's wrong) gcc was intended to be a main program that takes the input and decides what to do with it. So I'd better use gcc if it worked without -lstdc++. But if it's not possible I'll prefer using g++ instead as don't know what else gcc may miss.
Many thanks for your considerations
gcc is the GCC compiler-driver for C programs, g++ is the one for C++ programs.
Both will guess the language on the basis of the file-extension, unless overridden.
But if you use the wrong driver, the default-options will be wrong, like leaving out the C++ standard-library for C++ programs compiled with gcc when linking.
You can add just the library with -lstdc++, though using the proper driver is preferable, as plain gcc may be missing other, subtler options.

Calling C++ from Fortran with OpenMPI

I am having a compile-time issue which I have reduced to the following test case. I wish to call a C++ routine from fortran and have the C++ routine be MPI aware.
Consider the following sample code,
Fortran main:
! -- main.f90
program main
implicit none
external return_three
integer return_three
write(*,*) return_three()
end program main
C++ subroutine:
// -- subs.cpp
#include <mpi.h>
extern "C"
{
int return_three_();
}
int return_three_()
{
return 3;
}
Note that, for the problem to reproduce, I only need to include mpi.h.
Compiling with GCC 5.3 and OpenMPI 1.10.1 (I checked GCC 4.8 and PGI 15.10 too) gives the following problem during linking:
% mpic++ -c subs.cpp
% mpifort -c main.f90
% mpifort -o main subs.o main.o -lstdc++ -lgcc_s
subs.o: In function `MPI::Intracomm::Intracomm()':
subs.cpp:(.text._ZN3MPI9IntracommC2Ev[_ZN3MPI9IntracommC5Ev]+0x14): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Intracomm::Intracomm(ompi_communicator_t*)':
subs.cpp:(.text._ZN3MPI9IntracommC2EP19ompi_communicator_t[_ZN3MPI9IntracommC5EP19ompi_communicator_t]+0x19): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Op::Init(void (*)(void const*, void*, int, MPI::Datatype const&), bool)':
subs.cpp:(.text._ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb[_ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb]+0x24): undefined reference to `ompi_mpi_cxx_op_intercept'
subs.o:(.rodata._ZTVN3MPI3WinE[_ZTVN3MPI3WinE]+0x48): undefined reference to `MPI::Win::Free()'
subs.o:(.rodata._ZTVN3MPI8DatatypeE[_ZTVN3MPI8DatatypeE]+0x78): undefined reference to `MPI::Datatype::Free()'
collect2: error: ld returned 1 exit status
It seems to me like mpifort is missing some C++-related libraries. It's my understanding that mpifort should be used to compile a fortran main program, though. The problem doesn't occur with Intel 16.0 compiled against OpenMPI 1.10.1.
My questions are:
What's going on here? Why is Intel able to handle this sample code and PGI/GCC is not?
Is there a portable way to include C++ subroutines with MPI in a fortran code?
(if possible) Is there an easy way to fix my current problem? I'm trying to compile a package on my machine, so it'd be best if I could just add -lmagicfix or something.
I was able to compile your code with GCC 5.3.0 and openMPI 1.10.2 by adding -lmpi_cxx in the final step:
% mpic++ -c subs.cpp
% mpifort -c main.f90
% mpifort -o main main.o subs.o -lstdc++ -lmpi_cxx
The reason is that the openMPI wrapper compilers mpifort and mpic++ link to different MPI libraries. You can check this with the -showme:libs option:
% mpifort -showme:libs
mpi_usempif08 mpi_usempi_ignore_tkr mpi_mpifh mpi
% mpic++ -showme:libs
mpi_cxx mpi
So in order to use the C++ MPI library, you have to tell mpifort explicitly to link to it.

undefined reference error for g++ but not for gcc when using BSP library

I am making a parallel program for one of my courses. Now initially it was written in C and it all worked just fine. Then we changed it all to c++ and all of a sudden I get some error when I try to compile the program. I am using the package mcbsp (Multicore BSP) for the parallel part. Here is the program
#include <stdio.h>
#include <stdlib.h>
#include <mcbsp.h>
void sieve(){
return ;
}
main(int argc, int **arhv){
bsp_begin(5);
printf("%d\n",bsp_pid());
bsp_end();
return 0;
}
So I make a file aaa.c and aaa.cpp both with this content. Then when I type in the terminal
gcc aaa.c -MMD -g -I../bsp/include -L../bsp/lib -lmcbsp1.2.0 -lpthread
it compiles just fine and the program does as expected (print the id of the cores). However, when I type
g++ aaa.cpp -MMD -g -I../bsp/include -L../bsp/lib -lmcbsp1.2.0 -lpthread
it gives the following feedback
/tmp/ccH3uox9.o: In function `main':
/home/teun/Documents/C/BSP/Sieve/sieve/aaa.cpp:10: undefined reference to `bsp_begin(unsigned int)'
/home/teun/Documents/C/BSP/Sieve/sieve/aaa.cpp:11: undefined reference to `bsp_pid()'
/home/teun/Documents/C/BSP/Sieve/sieve/aaa.cpp:12: undefined reference to `bsp_end()'
collect2: error: ld returned 1 exit status
I have been stuck with this for about an hour now and it is really getting frustrating. I must admit that I have little experience in programming in c++ as I mostly use C. Does anyone know what is going wrong?
EDIT: The program I displayed is just a small working example that uses the BSP functionality.
The simple declaration int bsp_end(); is interpreted differently in C and C++. The reason is that in C++, you can overload functions, which requires that their parameters are included in the symbol name for the linker, see C++ name mangling for further info. Now, since the function is compiled with a C compiler but the declaration is given to a C++ compiler, the symbols don't match and you get linker errors.
As a solution, add extern "C" to the function declaration or maybe wrap the whole include:
extern "C" {
#include <mcbsp.h>
}
You need extern "C" around #include
extern " C" {
#include "c-header.h"
};
This is needed to stop the C++ name mangling

Undefined reference to __dso_handle_ - compiling C++ on cygwin

I have a basic Hello World C++ program that I am trying to compile and run on cygwin with g++. The code is:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world." << endl;
return 0;
}
Compiling it with: g++ helloWorld.cpp -o h results in the following error:
/tmp/ccDO1S4J.o:helloWorld.cpp:(.rdata$.refptr.__dso_handle[.refptr.__dso_handle]+0x0): undefined reference to__dso_handle'
collect2: error: ld returned 1 exit status`
I have been reading up on some other threads that indicate that it might be a linker problem and that invoking the linker separately or with verbose output might lead to some more clues.
So, I did:
1. g++ -c helloWorld.cpp -o helloWorld.o (this works - no errors).
2. ld -o h helloWorld.o causes a lot of undefined reference to __main or std::cout etc. errors.
I think this is a linking issue and that I need to link another library perhaps. Any pointers on how to solve this are most welcome.
Re-installing g++ via the installer application on cygwin worked.

Link dynamic shared library in Linux - Undefined reference to function

I know there are many questions related to shared libraries on Linux but maybe because I'm tired of having a hard day trying to create a simple dynamic library on Linux (on Windows it would have taken less than 10 minutes) I can't find what happens in this case.
So, I am trying to create a library to be linked at build-time and used at run-time (not a static library, not a library to be embedded into the executable, in other words). For now it contains a simple function. These are my files:
1.
// gugulibrary.cpp
// This is where my function is doing its job
#include "gugulibrary.h"
namespace GuGu {
void SayHello() {
puts("Hello!");
}
}
2.
// gugulibrary.h
// This is where I declare my shared functions
#include <stdio.h>
namespace Gugu {
void SayHello();
}
3.
// guguapp.cpp
// This is the executable using the library
#include "gugulibrary.h"
int main() {
GuGu::SayHello();
return 0;
}
This is how I try to build my project (and I think this is what is wrong):
gcc -Wall -s -O2 -fPIC -c gugulibrary.cpp -o gugulibrary.o
ld -shared -o bin/libGugu.so gugulibrary.o
gcc -Wall -s -O2 guguapp.cpp -o bin/GuGu -ldl
export LD_LIBRARY_PATH=bin
This is saved as a .sh file which I click and execute in a terminal. The error I get when trying to link the library is this:
/tmp/ccG05CQD.o: In function `main':
guguapp.cpp:(.text.startup+0x7): undefined reference to `SayHello'
collect2: ld returned 1 exit status
And this is where I am lost. I want the library to sit in the same folder as the executable for now and maybe I need some symbols/definitions file or something, which I don't know how to create.
Thanks for your help!
In your C++ file, GuGu::SayHello is declared as a C++ symbol. In your header, you are wrapping it in an extern "C" block. This is actually undefined, as you aren't allowed to use C++ syntax (namespace) in that context. But my guess is that what the compiler is doing is ignoring the namespace and generating a C symbol name of "SayHello". Obviously such a function was never defined by your library. Take out the extern "C" bits, because your API as defined cannot be used from C anyway.
You are inconsistent with your GuGu, there are also Gugu's running around, this needs to be made consistent, then it works (At least on my computer are some Gugu's now)