- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
1. I have SHKLibrary.c file with the following structure:
Is there anything I don't see to cause this error? Maybe using g++ and stdcall is not the correct combination; I also noticed that if the name of the library after the "-l" linker option is misspelled, the linker does not issue any warning or error.
[cpp]#include2. I create the object file SHKLibrary.o file using the following command (libSentinelKeys32.a is provided by a third party and it was tested to be OK):
#include
#include
#include
#include
#include "SentinelKeys.h"
#include "SentinelKeysLicense.h"
int main ( ) // does "main" have to be in the code ?
{
return 0;
}
extern "C" int CheckLicense ( )
{
// ... stuff needing Sentinel header files
return status;
}[/cpp]
[cpp]g++ -m32 -I . SHKLibrary.c -L . libSentinelKeys32.a -lpthread -L/usr/lib -o ./SHKLibrary.o[/cpp]3. I create a static library from the object file like this:
[cpp]ar rcs libSHKLibrary.a SHKLibrary.o[/cpp]4. The library libSHKLibrary.a is copied into the directory with my Fortran project which is compiled with following options:
[cpp]ifort -D_PARALLELIZATION_ -fixed -extend_source 132 -openmp -fpscomp general -warn declarations -assume byterecl -align all -heap-arrays -static-intel -threads -c "list of .for files"[/cpp]5. In one of my source code files (SHK.for), I have this:
[cpp]interface integer*4 function CheckLicense ( ) !dec$ attributes stdcall, decorate, alias: "CheckLicense" :: CheckLicense end function end interface[/cpp]and the call of the function in the form
[cpp]status = CheckLicense()[/cpp]6. I try to link the project using this command:
[cpp]ifort -L . -openmp "list of .o files" SHK.o -llibSHKLibrary.a "other .o files" -o myApp.out[/cpp]The linker issues the error "undefined reference to 'CheckLicense'".
Is there anything I don't see to cause this error? Maybe using g++ and stdcall is not the correct combination; I also noticed that if the name of the library after the "-l" linker option is misspelled, the linker does not issue any warning or error.
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If there is a Fortran main program, you don't need a C++ main(), but that doesn't appear to be the problem. If you didn't put -c option in your g++ compilation, you have made an a.out format result, not a linkable .o file.
Perhaps the compiler should tell you not to use stdcall; it looks like iso_c_binding would be a good choice, rather than dec$ attributes, but it may have (correctly) ignored stdcall.
Perhaps the compiler should tell you not to use stdcall; it looks like iso_c_binding would be a good choice, rather than dec$ attributes, but it may have (correctly) ignored stdcall.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The compiler does not ignore the STDCALL directive - it will still do the other things STDCALL does, including downcasing routine names and using pass by value. It does not change the actual calling mechanism.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I know this is an obvious question, but have you tried using "nm" to check the external name generated by Fortran, and the external name in the archive?
- Lorri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - jirina
1. I have SHKLibrary.c file with the following structure:
Is there anything I don't see to cause this error? Maybe using g++ and stdcall is not the correct combination; I also noticed that if the name of the library after the "-l" linker option is misspelled, the linker does not issue any warning or error.
[cpp]#include2. I create the object file SHKLibrary.o file using the following command (libSentinelKeys32.a is provided by a third party and it was tested to be OK):
#include
#include
#include
#include
#include "SentinelKeys.h"
#include "SentinelKeysLicense.h"
int main ( ) // does "main" have to be in the code ?
{
return 0;
}
extern "C" int CheckLicense ( )
{
// ... stuff needing Sentinel header files
return status;
}[/cpp]
[cpp]g++ -m32 -I . SHKLibrary.c -L . libSentinelKeys32.a -lpthread -L/usr/lib -o ./SHKLibrary.o[/cpp]3. I create a static library from the object file like this:
[cpp]ar rcs libSHKLibrary.a SHKLibrary.o[/cpp]4. The library libSHKLibrary.a is copied into the directory with my Fortran project which is compiled with following options:
[cpp]ifort -D_PARALLELIZATION_ -fixed -extend_source 132 -openmp -fpscomp general -warn declarations -assume byterecl -align all -heap-arrays -static-intel -threads -c "list of .for files"[/cpp]5. In one of my source code files (SHK.for), I have this:
[cpp]interface integer*4 function CheckLicense ( ) !dec$ attributes stdcall, decorate, alias: "CheckLicense" :: CheckLicense end function end interface[/cpp]and the call of the function in the form
[cpp]status = CheckLicense()[/cpp]6. I try to link the project using this command:
[cpp]ifort -L . -openmp "list of .o files" SHK.o -llibSHKLibrary.a "other .o files" -o myApp.out[/cpp]The linker issues the error "undefined reference to 'CheckLicense'".
Is there anything I don't see to cause this error? Maybe using g++ and stdcall is not the correct combination; I also noticed that if the name of the library after the "-l" linker option is misspelled, the linker does not issue any warning or error.
... you stare at something long enough ...
The command should be:
ifort -L . -openmp "list of .o files" SHK.o -lSHKLibrary.a "other .o files" -o myApp.out
Note: I removed the "lib" prefix from your library name.
The -l switchto ld (which is ultimately called by ifort) says "prefix this string with 'lib' and find me a library". That means it's looking for liblibSHKLibrary.a, and (of course) you don't have one with that name.
Alternatively, you can leave off the -l, and justlist "libSHKLibrary.a" as any other input file.
Finally, Yes, you are absolutely correct that there is no error/warning generated when ifort cannot find the library. That is being addressed by the development team.
- Lorri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Let me respond to your comments and suggestions:
- You are right that "main" does not have to be included.
- You are also right that "g++ ... -o ./name.o" does not produce a linkable .o file. That was one of errors I did.
- Another error was -llibName.a as Lorri pointed out. I should have read the documentation more thoroughly.
Anyway, I still have the same problem: "undefined reference". I decided to create a small example to see what could be wrong:
1. The C code is:
- You are right that "main" does not have to be included.
- You are also right that "g++ ... -o ./name.o" does not produce a linkable .o file. That was one of errors I did.
- Another error was -llibName.a as Lorri pointed out. I should have read the documentation more thoroughly.
Anyway, I still have the same problem: "undefined reference". I decided to create a small example to see what could be wrong:
1. The C code is:
[cpp]#include2. It is compiled and library libTest.a is created like this:extern "C" int LibFunc ( ) { printf("nLibrary function test output."); return 0; }[/cpp]
[cpp]g++ -m32 Library.c -c ar rcs libTest.a Library.o[/cpp]3. Fortran program is:
[cpp] PROGRAM LIBTEST interface integer*4 function LibFunc ( ) !dec$ attributes stdcall,decorate,alias:"LibFunc" :: LibFunc end function end interface integer*4 status status = LibFunc() call exit ( 1 ) END[/cpp]4. It is compiled and linked like this:
[cpp]ifort -c LibraryTest.for ifort -L . LibraryTest.o -lTest.a -o LibraryTest.out[/cpp]The linker issues the error "undefined reference to 'LibFunc'". I don't know what can be causing this problem. Is STDCALL appropriate for this example?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I got this to work:
ifort forum2.o -L. -lTest -cxxlib
(I called the Fortran program "forum2.for", in case that wasn't obvious)
Note: I took off the ".a" in the -lTest and had to add -cxxlib to tell the ifort driver that it should link against the C++ libraries.
Let us know how that works out, OK?
- Lorri
ifort forum2.o -L. -lTest -cxxlib
(I called the Fortran program "forum2.for", in case that wasn't obvious)
Note: I took off the ".a" in the -lTest and had to add -cxxlib to tell the ifort driver that it should link against the C++ libraries.
Let us know how that works out, OK?
- Lorri
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Lorri,
Indeed, your suggestions made both my simple example and my original application work as I originaly intended. Removing ".a" from -l string and adding the -cxxlib linker option worked like a miracle. :-)
Thank you very much for your quick and great help; I appreciate it a lot.
Indeed, your suggestions made both my simple example and my original application work as I originaly intended. Removing ".a" from -l string and adding the -cxxlib linker option worked like a miracle. :-)
Thank you very much for your quick and great help; I appreciate it a lot.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page