Static linking against .lib file generated by Visual studio lazarus / 21-General ---------------------------------------------------------------------- tihory · 2021-07-12 12:39 Hi, I wrote simple C++ code and built it with Visual Studio as a static library. Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i extern "C" int Add(int a, int b){        return a + b;}  When trying to link the generated lib against Pascal project, if using ".lib" file linker throws "Error: Invalid DLL SimpleStaticLib.lib, Dos Header invalid" error and if using the generated object file linker throws "Error: Illegal Big Obj COFF Magic while reading SimpleStaticLib.obj" error. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$linklib SimpleStaticLib.lib}//{$L SimpleStaticLib.obj} function Add(A, B: Integer):Integer; external; begin  Writeln(Add(10, 11));  Writeln('Hello World');end.  FPC libraries document : https://wiki.freepascal.org/Lazarus/FPC_Libraries What's problem and how to fix it? ---------------------------------------------------------------------- alpine · 2021-07-12 12:46 Quote from: tihory on July 12, 2021, 02:39:56 pm*snip* What's problem and how to fix it? Use TDM-GCC instead. ---------------------------------------------------------------------- marcov · 2021-07-12 12:53 Does it change if you use -Xe ? ---------------------------------------------------------------------- Zvoni · 2021-07-12 13:01 Having found a Thread in a german Lazarus-Forum, according to PascalDragon there (Compiler-Dev here), MSVS uses a slightly different format, which FPC doesn't understand. If i understood it correctly, try to compile your library in Visual Studio using an external compiler/linker (gcc, mingw) EDIT: And if your c++-Lib is as shown you're missing a "cdecl" in your external declaration ---------------------------------------------------------------------- tihory · 2021-07-12 14:10 Quote from: marcov on July 12, 2021, 02:53:05 pmDoes it change if you use -Xe ? -Xe doesn't change .lib linking behavior but for .obj file, linking failed with "SimpleStaticLib.obj: file not recognized: File format not recognized" error. Quote from: Zvoni on July 12, 2021, 03:01:17 pmHaving found a Thread in a german Lazarus-Forum, according to PascalDragon there (Compiler- Dev here), MSVS uses a slightly different format, which FPC doesn't understand. If i understood it correctly, try to compile your library in Visual Studio using an external compiler/linker (gcc, mingw) GCC and MINGW compile files to .a static library archive, that is supported by FPC but my C++ library doesn't support GCC and MINGW in windows. Quote from: Zvoni on July 12, 2021, 03:01:17 pmEDIT: And if your c++-Lib is as shown you're missing a "cdecl" in your external declaration CDecl causes runtime exception not compile time. ---------------------------------------------------------------------- BSaidus · 2021-07-12 15:04 .lib generated by MS visual C++ is not format compatible with gcc ones for ld.exe use gcc for windows. ---------------------------------------------------------------------- alpine · 2021-07-12 15:15 Quote from: tihory on July 12, 2021, 04:10:55 pmGCC and MINGW compile files to .a static library archive, that is supported by FPC but my C++ library doesn't support GCC and MINGW in windows.AFAIK in this case, the only option you have is to link it dynamically. ---------------------------------------------------------------------- PascalDragon · 2021-07-13 07:01 Quote from: tihory on July 12, 2021, 04:10:55 pmQuote from: Zvoni on July 12, 2021, 03:01:17 pmHaving found a Thread in a german Lazarus- Forum, according to PascalDragon there (Compiler-Dev here), MSVS uses a slightly different format, which FPC doesn't understand. If i understood it correctly, try to compile your library in Visual Studio using an external compiler/linker (gcc, mingw) GCC and MINGW compile files to .a static library archive, that is supported by FPC but my C++ library doesn't support GCC and MINGW in windows. You could try to simply compile your C++ library to object files using MSVC and then use ar from GCC/MINGW to generate a static library (the object files should be COFF after all in both cases). ---------------------------------------------------------------------- marcov · 2021-07-13 10:11 I remembered a SO post over this issue, and looked it up. -  Somebody said (in '13-'14) that mingw now supported .libs. -  They also pointed to a conversion tool lib2a       https://code.google.com/archive/p/lib2a/ Note that the first bit was why I suggested to use -Xe, but I forgot that our linker is from 2011. ---------------------------------------------------------------------- tihory · 2021-07-15 09:09 Quote from: PascalDragon on July 13, 2021, 09:01:18 amQuote from: tihory on July 12, 2021, 04:10:55 pmQuote from: Zvoni on July 12, 2021, 03:01:17 pmHaving found a Thread in a german Lazarus-Forum, according to PascalDragon there (Compiler-Dev here), MSVS uses a slightly different format, which FPC doesn't understand. If i understood it correctly, try to compile your library in Visual Studio using an external compiler/linker (gcc, mingw) GCC and MINGW compile files to .a static library archive, that is supported by FPC but my C++ library doesn't support GCC and MINGW in windows. You could try to simply compile your C++ library to object files using MSVC and then use ar from GCC/MINGW to generate a static library (the object files should be COFF after all in both cases). Thanks for your response, I tried to make an archive with "ar", but I've got "compile_error.obj: file format not recognized" error. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i file.exe .\compile_error.obj.\compile_error.obj: Intel amd64 COFF object file, not stripped, 87 sections, symbol offset=0x24e4, 320 symbols, created Thu Jul 15 08:41:50 2021, 1st section name ".drectve"  And if directly link obj file in source with $L directive getting "Fatal: Unsupported COFF symbol type 105 at index 55 while reading compile_error.obj" error. ---------------------------------------------------------------------- marcov · 2021-07-15 12:58 Are you compiling with 64-bit FPC ? ---------------------------------------------------------------------- tihory · 2021-07-17 07:28 Quote from: marcov on July 15, 2021, 02:58:07 pmAre you compiling with 64-bit FPC ? Yes, I'm using FPC 64 trunk/git. ---------------------------------------------------------------------- PascalDragon · 2021-07-17 17:54 First of you need to make sure that the project settings in Visual Studio the setting Whole Program Optimization (/GL) is set to No and that Debug Information Format is set to None. When you've built your static library using Visual Studio you then you execute the following command using the correct ar.exe for your bitness (best use the ar.exe provided with FPC): Code: [Select]ar.exe -cr libname.a objfile1.obj objfile2.obj ... The lib prefix and the .a suffix are important cause FPC will append them by itself. In your Pascal code you then add a {$LinkLib name, static} (name is the same part between the prefix and suffix you picked for the static library) and you pass the path to the directory of the library using the -Fl... option. In my tests that worked and produced a correctly working executable.