-2

I'm trying to build an app which uses sqlite3 for storing and fetching data, Till now I've used meson to generate my project and I'm now in a stage where I need VS 2022 for debugging purpose, while converting to VS, I'm stuck in producing a correct sqlite3 DLL, because when I use GCC to compile sqlite3.c (source file)

gcc -shared sqlite3.c -o sqlite3.dll

and use gendef or dumpbin to produce exports then a dummy .lib file to be linked inside VS project, It all works fine, But when I use VS 2022 to compile it as a DLL, both gendef and dumpbin fails

dumpbin /exports sql.dll
Microsoft (R) COFF/PE Dumper Version 14.33.31630.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file sql.dll

File Type: DLL

  Summary

        3000 .data
        E000 .rdata
        5000 .reloc
        1000 .rsrc
       AC000 .text

and

gendef - sql.dll
 * [sql.dll] Found PE image

I know sqlite3 offers a DLL (which is working fine), but why this problem exists or am I doing something wrong, PS I've also used build command from sqlite documentation, still the same error

cl sqlite3.c -link -dll -out:sqlite3.dll

even the above command didnt produce correct .def file

273K
  • 29,503
  • 10
  • 41
  • 64
  • msvc does not export symbols into a dll by default. See this answer for a good description: [https://stackoverflow.com/a/32284832/487892](https://stackoverflow.com/a/32284832/487892) – drescherjm Nov 27 '22 at 17:07
  • Don't understand. If you build a DLL using VS an acompanying LIB will be produced ,use that. – engf-010 Nov 27 '22 at 17:08
  • [https://developercommunity.visualstudio.com/t/automatically-export-symbols-from-dll-for-cc/1454513](https://developercommunity.visualstudio.com/t/automatically-export-symbols-from-dll-for-cc/1454513) – drescherjm Nov 27 '22 at 17:12
  • @drescherjm Thank you for replying so fast, Now I can see why its not working, I have to install MFC tool but my VS installer is not showing currently installed version, I will purge everything and install a new instance and will try the solution – Silver Surfer Nov 27 '22 at 17:49
  • Not an sqlite question – PChemGuy Nov 27 '22 at 17:49
  • 1
    @engf-010 while generating sqlite3.c as DLL its is not producing a .lib file, while it does produce .lib file while choosing static (as it should) and that lib is working fine. – Silver Surfer Nov 27 '22 at 17:50
  • 1
    `even the above command didnt produce correct .def file` You don't get a def or lib file if your code does not export any functions or data. – drescherjm Nov 27 '22 at 17:51
  • @PChemGuy sorry – Silver Surfer Nov 27 '22 at 17:52
  • I think the sqlite tag is somewhat relevant because there may be compiler definitions needed to be defined to have it use `__declspec( dllexport)` to export the symbols. I have not looked at the sqlite source in a while so I don't know. – drescherjm Nov 27 '22 at 17:54
  • @drescherjm yes I could see that now, I will try your soln ASAP, thank you – Silver Surfer Nov 27 '22 at 17:54
  • This may be the solution: [https://protyposis.net/blog/compiling-sqlite-as-dll-with-msvc/](https://protyposis.net/blog/compiling-sqlite-as-dll-with-msvc/) – drescherjm Nov 27 '22 at 17:56
  • @Silver Surfer: like drescherjm says in it's comment says : no lib generated ,then you haven't export anything. – engf-010 Nov 27 '22 at 17:56
  • Interestingly the sqlite documentation has the same build command listed in this question: [https://www.sqlite.org/howtocompile.html](https://www.sqlite.org/howtocompile.html) – drescherjm Nov 27 '22 at 17:58
  • What functions get exported by default varies between compilers. But all compilers have ways to control what is exported. You just need to learn how to drive each compiler. – Jesper Juhl Nov 27 '22 at 18:09
  • 1
    @drescherjm Thank you again, this [solution](https://protyposis.net/blog/compiling-sqlite-as-dll-with-msvc/) works. – Silver Surfer Nov 27 '22 at 18:36

1 Answers1

1
cl sqlite3.c -link -dll -out:sqlite3.dll

The line is documented in sqlite to compile an SQLite DLL. The problem is, it does not export any functions, does not create a .lib for native programming.

The solution is to export the API functions by setting the SQLITE_API preprocessor definition:

cl sqlite3.c -DSQLITE_API=__declspec(dllexport) -link -dll -out:sqlite3.dll

All credits goes to Mario and @drescherjm for finding this page https://protyposis.net/blog/compiling-sqlite-as-dll-with-msvc/

as mentioned by @drescherjm msvc does not export symbols into a dll by default. See this answer for a good description: https://stackoverflow.com/a/32284832/487892