error LNK2005: DllMain already defined in msvcrt.lib(dll_dll

I try to make a DLL with the pgfortran compiler. I first did the example in the manual (page 123), which worked with version 18.4 (I reported the bug in version 18.10 in January, and went back to version 18.4).

Now I want to do my own subroutine. Actually, i have several subroutines, but I want to export only three of them. I.e., a user should be able to call these three subroutines, but the other subroutines (which are only used internally) should be hidden from him. Hence I wrote three sources files like

subroutine sub1(x)
!DEC$ ATTRIBUTES DLLEXPORT :: sub1

end subroutine sub1

etc.

The project also uses libblas.lib and liblapack.lib. I am using Code::Blocks as an IDE.

When I build this project, I get the above error message. However, in no place in my source do I define a function “DllMain”. And this error does not occur with the example in the manual (which contains only a single subroutine and a common block that are exported).

Is this problem related to the fact that the DLL contains several exported subroutines? Or is the problem with the use of the lapack and blas libraries? I include the complete build-lob below.

best regards,
Bernhard

build log:

-------------- Build: Debug in MevelerDLL (compiler: PGI Fortran Compiler)---------------

pgfortran.exe -Mmakedll obj\Debug\src\AbelGuess.o obj\Debug\src\CopyVector.o obj\Debug\src\Entropy4Meveler.o obj\Debug\src\HistoricMEM2.o obj\Debug\src\Legend.o obj\Debug\src\LegendreProject3.o obj\Debug\src\MevelerTools.o obj\Debug\src\Modul_MevelerOptions.o obj\Debug\src\SimLeg.o obj\Debug\src\SimSC2.o obj\Debug\src\FetchOption.o obj\Debug\src\MevelerDLL3.o obj\Debug\src\SetOptions.o -o bin\Debug\MevelerDLL.dll -Mmakedll “…\Program Files\PGICE\win64\18.4\lib\libblas.lib” “…\Program Files\PGICE\win64\18.4\lib\liblapack.lib”
libblas.lib(memory.obj) : error LNK2005: DllMain already defined in msvcrt.lib(dll_dllmain_stub.obj)
Creating library bin\Debug\MevelerDLL.lib and object bin\Debug\MevelerDLL.exp
bin\Debug\MevelerDLL.dll : fatal error LNK1169: one or more multiply defined symbols found
Process terminated with status 2 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

You might want to try adding -Mnopgdllmain to your compile line. That option won’t link in the standard module that defines DllMain so you might be able to resolve the multiple-defined symbols error with that. I can try to put together an example later, but I have a feeling it is not related to including multiple subroutines or the blas/lapack libraries.

Let me know if that works.

Dear aglobus,

thank you for this hint. I tried it, but it did not resolve the problem. It seems to me that the problem is relatet to BLAS, I marked the line below is the build-log.

best regards,
Bernhard

-------------- Build: Debug in MevelerDLL (compiler: PGI Fortran Compiler)---------------

pgfortran.exe -Mmakedll obj\Debug\src\AbelGuess.o obj\Debug\src\CopyVector.o obj\Debug\src\Entropy4Meveler.o obj\Debug\src\HistoricMEM2.o obj\Debug\src\Legend.o obj\Debug\src\LegendreProject3.o obj\Debug\src\MevelerTools.o obj\Debug\src\Modul_MevelerOptions.o obj\Debug\src\SimLeg.o obj\Debug\src\SimSC2.o obj\Debug\src\FetchOption.o obj\Debug\src\MevelerDLL3.o obj\Debug\src\SetOptions.o -o bin\Debug\MevelerDLL.dll -Mmakedll -Mnopgdllmain “…\Program Files\PGICE\win64\18.4\lib\libblas.lib” “…\Program Files\PGICE\win64\18.4\lib\liblapack.lib”
libblas.lib(memory.obj) : error LNK2005: DllMain already defined in msvcrt.lib(dll_dllmain_stub.obj)
Creating library bin\Debug\MevelerDLL.lib and object bin\Debug\MevelerDLL.exp
bin\Debug\MevelerDLL.dll : fatal error LNK1169: one or more multiply defined symbols found
Process terminated with status 2 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Dear aglobus,

I made a small test example with three subroutines, two are exported, one is not. This does not produce the error. I used, in one of the subroutines, a call to the BLAS routine ddot, included IO, and allocate memory, but all of this does not create the error. So I have no idea what could cause it in my program. Interestingly, when I switch to GNU-fortran and use my own compilation of the OpenBLAS library, the DLL is built successfully. So perhaps the problem is still with the pg version of BLAS, but does not occur when only DDOT is used. Actually, I am using the Lapack routine DGELSD and the BLAS routine DGEMM.

best regards,
Bernhard

p.s.: is there a way to attach the test code? its only 30 lines, but I can not copy/paste it here?

Hi Bernhard,

Ah, yes. You’re right, I glossed over that. If you could send the reproducer to trs@pgroup.com I can take a peek at it.

I made a funny observation: When I reverse the order of the libraries, the error disappears:

-lblas -llapack : error message, linking aborts.
-llapack -lblas : successful compilation and linking. The program works as expected.

I will try to make an small example and send to the email address.

best regards,
Bernhard