Compile vs2010 under win7 to generate sqlite3.lib library

Knowledge Supplement:


      The sqlite3.dll dynamic link library, which allows programs to share code and other resources necessary to perform special tasks.

      The dynamic sqlite3.lib is equivalent to an h file, which is a declaration of the export part of the implementation part (.dll file). After compiling, only the export declaration part is compiled into the host program, and the generated sqlite3.exe needs corresponding dll file support at runtime.

The static sqlite3.lib puts both the export declaration and the implementation in the lib. After compiling, all the codes are embedded in the host program, and the generated sqlite3.exe can run directly.

Task:

     1. Use VS2010 to compile sqlite3, and generate dynamic sqlite3.lib and sqlite3.dll (sqlite3.dll can also be downloaded from the official website). Write a simple program in VS2010, using dynamic sqlite3.lib and sqlite3.dll.

     2. Use VS2010 to compile sqlite3 and generate static sqlite3.lib. Write a simple program in VS2010, using static sqlite3.lib. 


Task 1:


1. Download sqlite-amalgamation-3071000.zip and sqlite-dll-win32-x86-3071000.zip (the version I use) on the sqlite official website http://www.sqlite.org/download.html.

2. Unzip the above two files to their respective folders (sqlite3.def, sqlite3.dll are in the same folder sqlite-dll).

3. Find lib.exe and link.exe from Microsoft Visual Studio 10.0\VC\bin in the installation directory of VS2010, and find mspdb100.dll from G:\Microsoft Visual Studio 10.0\Common7\IDE in the installation directory of VS2010. Put lib.exe link.exe mspdb100.dll in the sqlite-dll folder in step 2.

4. Open the cmd window, go to the above sqlite-dll folder, mine is E:\SoftwareDesign\SQLite\sqlite-dll



5. Enter the command: LIB /DEF:sqlite3.def /machine:IX86. At this time, sqlite3.lib will appear in the sqlite-dll folder. (This is a dynamic sqlite3.lib file, only 47KB in size.) The following column generates sqlite3.lib for 64



6. Write a simple program in VS2010. File->New->Project->Win32 Console Application, named test1.

7. Replace the content in test1.cpp with the following code:
copy code
1  // test1.cpp : Defines the entry point for the console application.
2  //
 3 #include " stdafx.h " 
4 #include <stdlib.h>
 5 #include " sqlite3.h " 
6  int _tmain( int argc, _TCHAR* argv[])
 7  {
 8      int rc;
 9      int i, nrows, ncols, tr;
 10      char *errmsg = NULL;
 11      char ** results;
 12      
13      sqlite3 *db = NULL;
 14     rc = sqlite3_open("demodb", &db);
15     if (rc)
16     {
17         fprintf(stderr, "can't open db!\n", sqlite3_errmsg(db));
18         sqlite3_close(db);
19         exit(1);
20     }
21     else
22     {
23         printf("db open successfully!\n");
24     }
25     sqlite3_get_table(db,"select * from clients;",&results,&nrows,&ncols,& errmsg);
 26      printf( " DB has %d rows and %d cols\n\n " ,nrows,ncols);
 27      tr=(nrows+ 1 )* ncols;
 28      for (i= 0 ;i<tr;++i) // Output query results 
29      {
 30          printf( " results[%d]= %s/n " ,i,results[i]); // You can define the output format yourself here , 
31      }
 32      sqlite3_free_table(results); // free 
33      sqlite3_close(db);
 34      int a;
 35     scanf( " %d " ,&a); // Without this line of code, Ma Shan will disappear after the running window appears. 
36      return  0 ;
 37 }


View Code
8. Copy sqlite3.h, sqlite3.lib, and sqlite3.dll to the file directory where the project is located. Mine is E:\SoftwareDesign\SQLite\VisualStudio2010Workspace\Projects\test1\test1, as shown in the figure below:



9. In VS2010, right-click the test1 project, add -> Existing Item, and select the previous step in the dialog box that appears. sqlite3.lib. (The lib file must be added to the project)

10. Press the F5 key to run successfully.

PS: At this time, go to the file directory where the project is located and find test1.exe (29KB in size), mine is in E:\SoftwareDesign\SQLite\VisualStudio2010Workspace\Projects\test1\Debug. If you double-click to run it directly, it will prompt that it cannot run due to the lack of the sqlite3.dll file. This is because the dynamic sqlite3.lib just compiles the declaration part into test1.exe, which requires the support of the dll file at runtime. Copy the sqlite3.dll file to the same directory as test1.exe, and then double-click to run test1.exe, it will run successfully.

Task 2:

1. In VS2010, File->New->Project->Win32 Project, name it sqlite3 (other names are fine). In the wizard settings, select DLL(D), Empty Project.

2. Add the three files "sqlite3.h", "sqlite3.c" and "sqlite3ext.h" in the downloaded sqlite-amalgamation-3071000.zip to the project. The specific method is: right-click the sqlite3 project -> Add -> Existing items, select the above three files in the dialog that appears.

3. (The release version is smaller and runs faster than the debug version, so I released the release version in the operation) as shown below:


4. Press the F7 key to compile (or right->generate). The sqlite3.dll file will be compiled in the file directory where the project is located. (PS: Some explanations on the Internet need to configure a lot of parameters, etc., and have tried many times without success. However, according to the above, you can succeed without redundant operations. It may be due to the different versions of VS or sqlite)

5. When compiling, it will be The sqlite3.obj file is generated in the directory where the project is located, mine is under E:\SoftwareDesign\SQLite\VisualStudio2010Workspace\Projects\sqlite3\sqlite3\Release. Next, use the lib.exe file in the VS installation directory. Copy the sqlite3.obj file to the directory where lib.exe is located, mine is G:\Microsoft Visual Studio 10.0\VC\bin. Copy mspdb100.dll, mspdbcore.dll, mspdbsrv.exe, msobj100.dll from G:\Microsoft Visual Studio 10.0\Common7\IDE to the directory where lib.exe is located.

6. Open the cmd window, go to the folder where lib.exe is located, mine is G:\Microsoft Visual Studio 10.0\VC\bin. Enter the command: lib sqlite3.obj. At this time, a static sqlite3.lib (2.32MB in size) will be generated.



7. Write a simple program in VS2010. File->New->Project->Win32 Console Application, named test2.

8. Replace the code in test2.cpp with the above code. (code in step 7 of task 1)

9. Copy sqlite3.h and the static sqlite3.lib generated in step 6 to the file directory where the test2 project is located, mine is E:\SoftwareDesign\SQLite\VisualStudio2010Workspace\Projects\test2\test2.

10. In VS2010, right-click the test2 project -> Add -> Existing Item. Select sqlite3.lib.

11. Press the F5 key, the operation is successful.

PS: At this time, test2.exe (501KB in size) will be generated under E:\SoftwareDesign\SQLite\VisualStudio2010Workspace\Projects\test2\Debug. Double click to run directly. No dll file support is required at this time. Because the static sqlite3.lib puts the export declaration and implementation in the lib, all the codes are embedded in test2.exe after compilation and can be run directly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388858&siteId=291194637