How do I add sqlite3 to my project - c++

How do I statically link the sqlite3 libary to my C++ program?
I am using the G++ compiler.

in Unix/Linux you'll have to make sure the library (e.g. libsqlite3.a) is in your LD_LIBRARY_PATH and then you add "-lsqlite3 -static" to the g++ option.

Assuming you're on Linux and using the GNU ld linker:
g++ <your-code> -Wl,--Bstatic -lsqlite3
Of course, if libsqlite3.a isn't in your library path, you have to pass the directory it's in to the compiler as an additional -L flag.
If you don't have a static version (I don't on my system), you either have to check if you can get one or if you'll have to build your own.

On a Linux system I recommend using pkg-config.
Running pkg-config --cflags --libs --static sqlite3 should give you the compiler and linker flags you need.

Go to www.sqlite.org and download the latest version's amalgamation tarball. Include their source files to your project (make file, whatever) and forget about it. It's embedded anyway, they compile in a jiffy, if you put into your version control repo, you know what version you're using in what version of your application and you can forget about linking options. Just remember that their source files are C and not C++.

Related

C++ windows executable compiled with Linux needs additional DLL [duplicate]

I have been working on a project (a game to be specific) and I feel that I should start over with different libraries. So when doing this I reinstalled Code::Blocks and setup my new libraries and includes.
But as of now Im having a problem starting u[ my new project to test if all of the includes work. This problem is: libstdc++-6.dll was not found. At first i wondered if I could just find this file online, but its nowhere to be found(or at least the many places I have searched...) Soon after, I tried loading up my old project, and the same problem happened again(wierd... ._.) I was thinking its maybe my compiler, so I used my older compiler and it did the same thing! At this moment I held the problem off for tomorrow(which is today)
So my question is: If anyone else had this problem, how would you solve it?
Im using Code::Blocks with MinGW as the compiler on Windows Vista 32 bit.
*****EDIT*****
Here are the Build options in my project. Note that these are the settings in the Project, not the global compiler:
In (project name)->Compiler settings->Otehr options:
(I use // to seperate the commands)
-mthreads//
-fmessage-length=0//
-fexceptions//
-fident//
In (project name)->Compiler settings->#define:
WIN32//
_WINDOWS//
In (project name)->Linker settings->Other linker options:
-static-libstdc++//
-static-libgcc//
-Wl,--enable-auto-image-base//
-Wl,--add-stdcall-alias//
-Wl,--enable-auto-import//
In linker->link libraries i have various links to files with a .a extension, these files include Bullet PHysics, Ogre3D, and SFML
In the search directories i have links to the MinGW/bin, and the MinGW/lib directories, along with other links to different libraries.
My Compiler is MinGW, a GNU GCC compiler for windows 32 bit. and the IDE is Codeblocks. Also note that in Debug and Release settings on the project, there is nothing.
Most of these setings are also pieces that i got from the Ogre3D Application setup tutorial if that is of any help.
If you are using MingW to compile C++ code on Windows, you may like to add the options -static-libgcc and -static-libstdc++ to link the C and C++ standard libraries statically and thus remove the need to carry around any separate copies of those. Version management of libraries is a pain in Windows, so I've found this approach the quickest and cleanest solution to creating Windows binaries.
As far as I know, this is the C++ Runtime Library. So it depends on the compiler you use to create your program (A new version will include some C++0x stuff, an older version will probably not for instance. It depends of the compiler and of its version).
If you use MinGW then you should use the libstdc++-6.dll found into the folder of this compiler. MinGW/bin folder should be the place to search for it on your computer.
If you copy this file in the same directory as your executable, it should be OK.
Simply removing libstdc++-6.dll.a \ libstdc++.dll.a from the mingw directory fixes this.
I tried using the flag -static-libstdc++ but this did not work for me.
I found the solution in: http://ghc.haskell.org/trac/ghc/ticket/4468#
This error also occurred when I compiled with MinGW using gcc with the following options:
-lstdc++ -lm, rather than g++
I did not notice these options, and added: -static-libgcc -static-libstdc++
I still got the error, and finally realized I was using gcc, and changed the compiler to g++ and removed -stdc++ and -lm, and everything linked fine.
(I was using LINK.c rather than LINK.cpp... use make -pn | less to see what everything does!)
I don't know why the previous author was using gcc with -stdc++. I don't see any reason not to use g++ which will link with stdc++ automatically... and as far as I know, provide other benefits (it is the c++ compiler after all).
useful to windows users who use eclipse for c/c++ but run *.exe file and get an error: "missing libstdc++6.dll"
4 ways to solve it
Eclipse ->"Project" -> "Properties" -> "C/C++ Build" -> "Settings" -> "Tool Settings" -> "MinGW C++ Linker" -> "Misscellaneous" -> "Linker flags" (add '-static' to it)
Add '{{the path where your MinGW was installed}}/bin' to current user environment variable - "Path" in Windows, then reboot eclipse, and finally recompile.
Add '{{the path where your MinGW was installed}}/bin' to Windows environment variable - "Path", then reboot eclipse, and finally recompile.
Copy the file "libstdc++-6.dll" to the path where the *.exe file is running, then rerun. (this is not a good way)
Note: the file "libstdc++-6.dll" is in the folder '{{the path where your MinGW was installed}}/bin'
I use Eclipse under Fedora 20 with MinGW for cross compile.
Use these settings and the program won't ask for libstdc++-6.dll any more.
Project type - Cross GCC
Cross Settings
Prefix: x86_64-w64-mingw32-
Path: /usr/bin
Cross GCC Compiler
Command: gcc
All Options:
-I/usr/x86_64-w64-mingw32/sys-root/mingw/include -O3
-Wall -c -fmessage-length=0
Includes: /usr/x86_64-w64-mingw32/sys-root/mingw/include
Cross G++ Compiler
Command: g++
All Options: -I/usr/x86_64-w64-mingw32/sys-root/mingw/include -O3
-Wall -c -fmessage-length=0
Includes: /usr/x86_64-w64-mingw32/sys-root/mingw/include
Cross G++ Linker
Command: g++ -static-libstdc++ -static-libgcc
All Options: -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/bin
Library search path (-L):
/usr/x86_64-w64-mingw32/sys-root/mingw/lib
/usr/x86_64-w64-mingw32/sys-root/mingw/bin
I just had this issue.. I just added the MinGW\bin directory to the path environment variable, and it solved the issue.
I placed the libstdc++-6.dll file in the same folder where exe file is generated.
You only need to add your "mingw-install-directory"/bin/ to your Path in your System environment variables ... that's it !!
I had same problem. i fixed it. i was using Codeblocks and i save my .cpp file on desktop instead of saving it in Codeblocks file where MinGW is located. So i copied all dll files from MinGW>>bin folder to where my .cpp file was saved.
You can also copy the dll files in the directory of your exe file
I had this problem too. I was compiling in command prompt and used the flag -static.
My command before:
"g++ test.cpp -o test.exe"
and afterwards:
"g++ test.cpp -o test.exe -static"
I had the same problem and I solved it by running the compiled exe as an administrator.

OpenCV C++ Linux g++ compiling

I just installed OpenCV-3.4.1 on Ubuntu 18.04. I am able to compile my C++ files only when I run the g++ command with pkg-config --cflags --libs opencv
Is it possible for me to compile the c++ file without using these additional flags
How can I tell g++ to automatically look at /usr/include/opencv for the .h files everytime
For adding to the include path see this question: How to add a default include path for GCC in Linux?
A better solution however is to write a shell script to compile your code rather than having to type in the command line every time.
The best solution is to use a proper build system which will save you a lot of pain in the future, just a few of the many available options:
GNU make
cmake
google gyp
google gn
ninja

How to tell C++ library path in Cygwin and MinGW

I develop C++ programs using a Cygwin installation on top of Windows XP.
I also have MinGW installed, because I want to use it's version of g++, not the one that comes with Cygwin.
That part seems to be set up correctly. When I start a Cygwin session I see this:
$ which g++
/cygdrive/c/MinGW/bin/g++
This is correct, g++ is pointing to my MinGW install.
What I don't understand is when I write code that includes library code (for example, header files from the `Winsock/BerkleySockets API), how can I tell where the compiler is finding that header file?
For example, if I have #include "winsock.h" in my code, where does the compiler find that header file?
If I do a general search for winsock.h on my computer, I get this:
C:\MinGW\include
C:\cygwin\usr\include\w32api
Both have a copy of winsock.h (though the file sizes of these aren't exactly the same, so they can't be identical).
Thanks for the help.
I should also point out, I have the C:\MinGW\bin in my Windows PATH Environment Variable, as well as that same path configured in my/etc/profile file within Cygwin.
I'm guessing the g++ compiled for MingW has the same command line arguments as the standard g++. Check out the g++ manual page.
To add include paths to your compilation, use the -I flag.
g++ -I/include/path/here -I/another/include/path -o prog src.cpp
To add library paths to your linking, use the -L flag.
g++ -L/lib/path/here -L/another/lib/path -o prog src.cpp
The MingW site explains how the include file search works on MingW, and how to modify it.
The site also says that if you want to view the include file search while it happens during the compilation, pass the verbose flag (-v) to the compiler.
g++ -v -o prog src.cpp
I believe it's referring to the one in MinGW/include. Take a look at the Minigw documentation for include paths.
If you are using an eclipse environment you can specify the include paths in the project settings along with specifying your choice of compiler i.e mingw in your case. Let us know if you still have a problem.

How to link wsock32 library through g++?

I'm using minGW on windows, trying to compile a c++ program. I've used sockets in there, so I'm trying to link (not include... I've already included winsock.h) the wsock32 library. I know that the -L switch is for linking but none of the following commands work:
g++ C:\program.cpp -Lwsock32.lib
g++ C:\program.cpp -LC:\windows\system32\wsock32.dll
g++ C:\program.cpp -lC:\windows\system32\wsock32.dll
g++ C:\program.cpp -LC:\windows\system32\wsock32.lib
what should I do??
The -L option is for setting the directory where the linker should look for libraries/dlls.
The -l option is for naming the libraries/dlls you want to link with.
That would mean
g++ C:\Program.cpp -LC:\Windows\System32 -lwsock32
should be the command to compile your program from your regular windows command prompt.
I suspect your compiler may look in system32 automatically, so you may want to just try to skip the -L option.
As #Joshua commented, you probably want ws2_32.dll.
The GNU Compiler Collection uses ranlib archives (A files) rather than Visual Studio LIB files.
The w32headers project provides gcc- and g++-compatible headers and archives for most standard Windows DLLs, including ws2_32.dll. The name of the archive is usually the name of the DLL minus the .dll extension, prefixed with lib and suffixed with .a (following the *nix archive naming convention). Thus, the symbols for ws2_32.dll are in libws2_32.a, which can be linked with using ‑lws2_32.
By default, the w32headers archives are in the GNU ld library path, so to be able to link with standard Windows DLLs, there is no need to add library directories with an ‑L option. In your case, the only option that you need is ‑lws2_32:
g++ C:\Program.cpp -lws2_32
Assuming that compilation and linking succeed, the output will be a.exe in the current directory. You can override the name of the output binary with the ‑o option.
NOTE: I used non-breaking hyphens in my answer. If you copy & paste the command line options, be sure to replace all hyphen-looking characters with regular hyphens.

How to link to debug versions of binary libs

If I pass a lib name with -l option, then GCC will link to the best lib from /usr/lib. I think the dynamic version is chosen over the static version when both are present.
But there are also debug versions of the same lib. Debug versions are supposed to be compiled with optimisations off and extra runtime checks on. I checked it with the package manager (Synaptic on Ubuntu) that dbg versions are indeed installed on my computer, but not entirely sure where are those libs and how to link to them.
Any hints are greatly appreciated. Thanks.
If you are aware of the name of the debug library, you can directly use it in compilation instead of going with -l option. For example :
gcc -o <output_file> *.c <libdebug_file>.so
how about moving those libraries to a different path rather than standard ones and then compile using g++ with the -l (library-name option)