Search code examples
c++sqlitemingw-w64

Compiling SQLite amalgamation file with C++ file using minGW-w64


I'm just beginning to experiment with C/C++ and SQLite, and installed the minGW-w64 compiler and was able to get the little "Hello, world." code to compile to an .exe file and run it.

Then I added the sqlite3.c amalgamation file to the directory and attempted to compile it with the helloworld.c file, and the first result was many, many rows of errors and warnings, it appears because I used g++ instead of gcc.

I have two very basic questions.

  1. Is it correct to always use the sqlite3.c amalgamation file in this manner, compiling it along with the rest of the code, even though it is a rather large file. The SQLite web site states, "The use of the amalgamation is recommended for all applications" and "makes [them] run faster."

The .exe file was 53kb for hello world compiled alone and 1,015kb for the combination even though no SQLite statements were used.

  1. Why did using g++ generate the large error list and gcc did not generate any? I realize that g++ is for c and c++ and sqlite3 is c. Does this mean that a c++ file and sqlite3.c cannot be compiled together?

Thank you.


Solution

  • Yes, bundling sqlite like that is intended behavior.

    You can compile sqlite3.c with a C compiler first and link the result into a binary:

    gcc -O2 -c sqlite3.c
    g++ -O2 sqlite3.o myapp.cpp -o myapp.exe
    

    I strongly suggest automating this project with a simple makefile or Cmake.