Error: undefined reference to `sqlite3_open' - c++

I'm trying to get started with the C++ API for SQLite.
#include <iostream>
#include <sqlite3.h>
using namespace std;
int main()
{
sqlite3 *db;
if (sqlite3_open("ex1.db", &db) == SQLITE_OK)
cout << "Opened db successfully\n";
else
cout << "Failed to open db\n";
return 0;
}
Compiling this using the command "g++ main.cpp" gives the following error:
/tmp/ccu8sv4b.o: In function `main':
main.cpp:(.text+0x64): undefined reference to `sqlite3_open'
collect2: ld returned 1 exit status
What could have gone wrong? Hasn't sqlite3 properly installed in the server I'm compiling this in?

You need to link the sqlite3 library along with your program:
g++ main.cpp -lsqlite3

You need to adjust your linker flags to link in the sqlite3 library. Libraries are usually installed in /usr/lib or /usr/lib64
Alternatively, you can copy the sqlite3.c file to your project directory and compile it as part of the g++ command:
g++ main.cpp sqlite3.c
as per: http://sqlite.org/cvstrac/wiki?p=HowToCompile

First step: Install all library sqlite3 with the command:
sudo apt-get install libsqlite3-dev
With that you can use #include <sqlite3.h> in a programm of C or C++.
Second step: To compile the program by console:
C++:
g++ program.cpp -o executable -lsqlite3
./executable
C:
gcc program.c -o executable -lsqlite3
./executable

Either link your program to lib g++ yourProgram.c -lsqlite3 in command line or in Open IDE -> project -> properties -> locate lib file for sqlite3 .

Compile using Devcpp
1. add sqlite3.dll file in the project folder.
2. go to Compiler option in Tools >>
3. write sqlite3.dll next to >> Add the following commands when calling compiler
Compile using command line
NOTE : install MinGW (compiler)
g++ file.cpp -o output.exe sqlite3.dll
Compile using VS
define sqlite3.dll in linker in project properties

Related

undefined reference to `sqlite3_open'. I out of ideas

Im sorry for the bad title.
I want to get started using sqlite with c++. So i downloaded the amalgamation from the site and compiled to get the .dll
gcc -shared sqlite3.c -o sqlite3.dll
I included the sqlite.h file in my project and the .dll file too. I compiled:
g++ prueba.cpp
and got this error message
C:\Users\PABLOS~1\AppData\Local\Temp\ccUI3YAt.o:prueba.cpp:(.text+0x2d): undefined reference to `sqlite3_open'
C:\Users\PABLOS~1\AppData\Local\Temp\ccUI3YAt.o:prueba.cpp:(.text+0x41): undefined reference to `sqlite3_errmsg'
collect2.exe: error: ld returned 1 exit status
Ok I said, lets see in stack overflow. In some question that I read they recomended to do this:
g++ main.cpp sqlite3.c
But the output was a really long list of error messages. I kept on reading but most of the questions where solved by:
sudo apt install libsqlite3-dev
or
gcc main.c -lsqlite3
In one of the questions the same guy that asked answered that he didnt include the .a file. So i googled about it and followed the instructions in this article. I created the .def file:
dlltool -z sqlite3.def --export-all-symbols sqlite3.dll
And created the .a file
dlltool -d sqlite3.def -l libsqlite3dll.a
Then included it in C:\MinGW\lib and tried again to compile
g++ prueba.cpp -lsqlite3dll
And i got the same error message. At this point im kind of lost (Im new to programing), and i dont know what to do next. Can you give me a pointer in the direction I should head in?
Edit: Answered a question form the coments
// This is my code
#include <iostream>
#include "sqlite3.h"
int main(int argc, char **argv) {
// Esto es lo que necesitamos para abrir la base de datos
sqlite3 *db;
char *zErrMsg = NULL;
int rc;
// La abrimos y revisamos por errores
rc = sqlite3_open("test.db", &db);
if (rc) {
std::cerr << "No se pudo abrir la base de datos: " << sqlite3_errmsg(db);
return 1;
}
std::cout << "Se pudo abrir la base de datos!"<< std::endl;
std::cin.get();
return 0;
}
Bitten by this issue again today when working with g++.
Ensure the following:
'lib' prefix is part of the static link library name "libsqlite3.lib" (my issue was that I was compiling sqlite3.def into sqlite3.lib and when passed to g++ using -l option it was failing to resolve function references during linking, the g++ compiler requires the library name to be prefixed with 'lib' as in libsqlite3.lib and associated g++ command should be of the form: g++ -lsqlite3 ...
Ensure Library search path is valid when using -L option
ex: g++ -Lc:\static_libs -lsqlite3 -o app.exe main.cpp.
In this case the library 'libsqlite3.lib' must be located as 'c:\static_libs\libsqlite3.lib'
Or specify full path to the lib file without using -l or -L options
ex: g++ -o app.exe main.cpp c:/static_export_libs/sqlite3.lib
In case you only have the precompiled shared library sqlite3.dll and 'sqlite3.def', you can simply create exports file libsqlite3.exp and its associated static link library libsqlite3.lib using following sample command:
lib.exe" /machine:x64 /def:sqlite3_x64/sqlite3.def /out:sqlite3_x64/libsqlite3.lib
lib.exe" /machine:x86 /def:sqlite3_x86/sqlite3.def /out:sqlite3_x86/libsqlite3.lib
Instead of:
gcc -shared sqlite3.c -o sqlite3.dll
run:
gcc -shared sqlite3.c -o sqlite3.dll -Wl,--out-implib,libsqlite3.dll.a
This will give you both a .dll binary and a .dll.a library file.
Then instead of:
g++ prueba.cpp
run:
g++ -shared -o prueba.exe prueba.cpp -lsqlite3
If your .exe and .dll are in the same folder you should be able to run the .exe.

gmp.h missing when trying to use boost library

I am trying to use the boost library with QT on windows. I've successfully build the library and also managed to include it in my project. However, on including gmp (#include "boost/multiprecision/gmp.hpp") and creating an object (boost::multiprecision::mpz_int myint;) I get the following error:
C:\Users\Laurenz\Documents\libraries\boost_1_66_0\include\boost\multiprecision\gmp.hpp:31: error: gmp.h: No such file or directory
And indeed, I haven't been able to find any such file in the boost directory. What did I do wrong?
Install the dependency and link to it. (See What is an undefined reference/unresolved external symbol error and how do I fix it?)
Alternatively, consider not using GMP, using cpp_int.hpp instead.
Since you already installed the GMP library, here's the last step:
Live On Coliru
#include <boost/multiprecision/gmp.hpp>
#include <iostream>
int main() {
boost::multiprecision::mpz_int i("1238192389824723487823749827349879872342834792374897923479");
std::cout << pow(i, 3) << "\n";
}
Note the -lgmp flag at the end of the compile/link command:
g++ -std=c++11 -O2 -Wall -Wextra -pedantic main.cpp -o demo -lgmp
Running it:
./demo
1898298004808110659499396020993351679788129852647955073547637871096272981567489303363372689896302906549189545322451852317205769760555889831589125591739044248515246136031239

Fresh installation of bullet physics engine is not linking properly?

Have a relatively new and fresh installation of ubuntu LTS, I followed the instructions found here for generic dynamic library building: http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Installation
Installation appears to go smoothly (no errors or warnings) and afterwards I have all of the necessary files located in /usr/local/include/bullet. I'm currently attempting to compile a file from the following shell command:
g++ bulletTest.cpp -lGL -lGLU -I /usr/local/include/bullet/
bulletTest.cpp is:
#include <iostream>
#include <btBulletDynamicsCommon.h>
int main (void)
{
btBroadphaseInterface* broadphase;
return 0;
}
This file has nothing to it, but is throwing a plethora of undefined reference errors, such as "/tmp/ccH09wLR.o:(.rodata._ZTV17btTypedConstraint[_ZTV17btTypedConstraint]+0x30): undefined reference to `__cxa_pure_virtual'" I cannot find any other complaints on the forums/SO so I believe it must be a problem with my build/installation. These types of errors occur while attempting both gcc and g++ compilers.
Anybody have an idea?
EDIT: changed my shell command to g++ bulletTest.cpp -lGL -lGLU -lBulletDynamics -lBulletCollision -lBulletSoftBody -lLinearMath -I /usr/local/include/bullet/
It compiles but does not run, error is: error while loading shared libraries: libBulletDynamics.so.2.82: cannot open shared object file: No such file or directory
So it was a lot of little stuff. Firstly, don't reference "lbullet" reference each of the libraries needed:
g++ bulletTest.cpp -o bulletTest -lGL -lGLU -lBulletDynamics -lBulletCollision -lBulletSoftBody -lLinearMath -I /usr/local/include/bullet/
In addition, I had to set the LD_LIBRARY_PATH variable
export LD_LIBRARY_PATH=/usr/local/lib
and then it would both compile and run.

Difficulty linking Boost 1.60 on OS X after installing via Homebrew

I have a fresh install of OS X 10.11.4 that I immediately installed the Xcode toolchain on, then Homebrew, then Boost 1.60. In order to test that everything had gone well, I wrote the following code on my Desktop.
#include <iostream>
#include <boost/filesystem.hpp>
int main() {
boost::filesystem::path new_directory("hello");
boost::filesystem::create_directory(new_directory);
}
I then attempted to compile it as I usually have done with the following command.
$ clang++ test.cpp -o test -lboost_system -lboost_filesystem
I received the following error.
test.cpp:3:10: fatal error: 'boost/filesystem.hpp' file not found
#include <boost/filesystem.hpp>
This is how I have always compiled projects that link Boost in the past. I'm assuming that I have probably forgotten a step along the way that allows clang to search a specific path to dynamically link the libraries. What should I change in order for this compilation command to work?
For me, boost has been compiled and installed into a subdirectory of my home directory, so you'll need to modify the paths as appropriate for your homebrew installation:
flags="-std=c++1z -I/${HOME}/local/include -L${HOME}/local/lib -lboost_filesystem -lboost_system"
c++ ${flags} -o jared jared.cpp
First get the location of boost by doing the following:
brew info boost
From the image above, you can see that my location is
/usr/local/Cellar/boost/1.66.0
Then, to compile, use the following:
c++ -I /usr/local/Cellar/boost/1.66.0 main.cpp -o boost

Cannot connect to PostgreSQL using C++

I am trying to run PostgreSQL on my mac. PostgreQL itself works fine and I can create database and table and stuff but when I try to connect to PostgreSQL using C++ with something like:
#include <stdio.h>
#include </Library/PostgreSQL/8.4/include/libpq-fe.h>
#include <string>
int main() {
PGconn *conn;
PGresult *res;
int rec_count;
conn = PQconnectdb("dbname=ljdata host=localhost user=dataman);
if (PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
res = PQexec(conn, "update people set phonenumber=\'5055559999\' where id=3");
and compile with something like:
g++ -lpq db.cpp -o db
I get the error
ld: library not found for -lpq
and if I compile without lpq, I get
Undefined symbols:
"_PQclear", referenced from:
_main in ccpjNCAU.o
_main in ccpjNCAU.o"
I have already included the libpq-fe.h, shouldn't it work? Does anybody know what went wrong?
g++ can't find the pq library. You have to specify where to look for it, with a capital -L:
g++ -L/path/to/pq/lib -lpq db.cpp -o db
where pq is /path/to/pq/lib/libpq.a (or whatever the extension is)
Here's what you probably want to do:
change the include line to not have the path.
#include "libpq-fe.h"
Add the include path to the commandline
g++ -I/Library/PostgreSQL/8.4/include db.cpp
Build intermediary object files
g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -o db.o
Link it together as a separate step
g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq
Build with debug info using -g
Put it all together, for two separate build steps: compile and link:
g++ -I/Library/PostgreSQL/8.4/include db.cpp -c -g -o db.o
g++ -L/Library/PostgreSQL/8.4/lib db.o -lpq -o db
libpq-fe.h is a user library, not a system library, and therefore you should use "..." instead of <...>, like this:
#include "/Library/PostgreSQL/8.4/include/libpq-fe.h"
Take a look at this link. And make sure libpq-fe.h can actually be found by your compiler.
Had the same problem, You need to add the path of the library to /etc/ld.so.conf, do it and you'll see.
Good luck