make with g++/intel C++ compiler calling fortran functions
I have been trying to call some functions from a library provided by a commercial finite element code. The examples on using this library are being compiled with intel fortran compiler for my type of system so I am using intel c++ compiler to link the object files, I have set up a simple test case with the extern declerations as
extern "C"
{
int binini_( int* );
int biniqr8_( int*, int* );
i开发者_如何学Cnt binset_( int*, int*, int*, int*, int*, int*, const char*, int*, int*, int* );
int binrd8_( int*, int*, int*, int*, int*, int* );
int binclo_( int*, const char*, int* );
}
And now, I am trying to use intel c++ compiler with the below makefile:
CXX = icpc -parallel
IncludeDir = /home/utabak/external_libraries/boost_1_46_1
LinkingDir0 = /home/utabak/external_libraries/boost_1_46_1/stage/lib
LinkingDir1 = /opt/ansys-12.1/v121/ansys/customize/misc/linx64
LibLink1 = bin
LibLink2 = boost_filesystem
LibLink3 = boost_system
all: test1
test1: test_binlib1.o
${CXX} -o $@ $? -L${LinkingDir0} \
-L${LinkingDir1} \
-l${LibLink1} -l${LibLink2} -l${LibLink3}
test_binlib1.o: test_binlib1.cc
${CXX} -I${IncludeDir} -c $?
which links and compiles fine. And run as expected. I wanted to do the same with the g++ also, after some trial and error, I set up a make file that is compiling and linking fine:
CXX = g++ -g -pg -Wall -pthread
IncludeDir = /home/utabak/external_libraries/boost_1_46_1
LinkingDir0 = /home/utabak/external_libraries/boost_1_46_1/stage/lib
LinkingDir1 = /opt/ansys-12.1/v121/ansys/customize/misc/linx64
LinkingDir2 = /home/utabak/intel/composerxe-2011/lib/intel64
LibLink1 = bin
LibLink2 = boost_filesystem
LibLink3 = boost_system
LibLink4 = iomp5 # an extra library from intel compiler
# openmp, for intel C++ -parallel does it
# for g++ I have to explicitly link this
all: test1
test1: test_binlib1.o
${CXX} ${LFLAGS} -o $@ $? -L${LinkingDir0} \
-L${LinkingDir1} -L${LinkingDir2} \
-l${LibLink1} -l${LibLink2} -l${LibLink3} -l${LibLink4}
test_binlib1.o: test_binlib1.cc
${CXX} -I${IncludeDir} -c $?
Hoever, this executable does not run correctly, and strangely, it some times gives a segmentation fault, or file open error, where I first try to open a file using the above mentioned fortran library function, binset_ .
I am puzzling on the reason of these errors and the differences. What could be the answer to this problem?
I think you might be getting caught by the strings you are passing. When passing strings between Fortran and C++, you need to account for the hidden length argument. The prototypes you show for binset_ and binclo_ do not include this.
By default, when Fortran passes arguments, they are all passed by reference except the hidden length argument of the string which is passed by value. This hidden length argument is tacked onto the end of the argument list.
Consider the Fortran routine:
SUBROUTINE BLAH(MYSTRING)
The C++ side needs:
void blah_ (char *mystring, unsigned int mystringlength)
精彩评论