undefined reference to `sqlite3_open'. I out of ideas - c++

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.

Related

Linking static C++ archive file in Hello World app

I'm trying to link an archive file into my simple hello world app, to make sure I understand the process. However, I obviously don't, because the library isn't linking correctly.
Here is my simple app (hello.cc):
#include <iostream>
#include "firebase/app.h"
int main()
{
std::cout << "Hello, world!\n";
std::cout << std::endl;
return 0;
}
And here is my compilation command with linking:
gcc hello.cc -L /tmp -l app -o hello -lstdc++
I've moved my archive file (libapp.a) to /tmp.
My understanding is that -L <dir> adds directories to search for lib*.a files and -l <name> indicates the name of the archive files, in the form of lib<name>.a. I know it isn't linking the archive file, because I get this error:
hello.cc:3:26: fatal error: firebase/app.h: No such file or directory
#include "firebase/app.h"
However, I also don't think the compilation command does what I think it does, because I renamed libapp.a to libapp.a2 and the same error was returned, not something indicating that the archive file was missing.
Can somebody help me with a) the command to link the library file /tmp/libapp.a and b) explain what I'm doing wrong?
EDIT
I forgot to include the headers, which caused the initial error I think. Now that I'm including the headers, with the following:
gcc hello.cc -L /tmp -l app -I /tmp/firebase/include -o hello -lstdc++
I get this new error:
/usr/bin/ld: cannot find -lapp
collect2: error: ld returned 1 exit status
So, I think I'm not using -L and -l correctly, but not sure what exactly I'm doing wrong.
EDIT2
Fixed. Forgot I had renamed the lib file while trying to figure out the first issue. Once I named it back to libapp.a the app worked as expected.

/usr/bin/ld cannot find -l<nameOfLibrary>

I have written a few programs and while trying to compile them using g++,as thus,
$ g++ minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp -L/usr/local/lib/ -lfwlib32 -lpthread -o adapter
I keep getting the following error:
/usr/bin/ld: cannot find -lfwlib32
collect2: error: ld returned 1 exit status
fwlib32.h is the library I am trying to include. The shared object file libfwlib32.so is present in /usr/local/lib as well as /usr/lib. But I am unable link to it. I have tried all the solutions offered by similar questions including
$ export LIBRARY_PATH=/usr/local/lib/
$ export LD_LIBRARY_PATH=/usr/local/lib
I have done the above for /usr/lib as well, but still the same error.
I have tried using the -L option in the command line but I still get the error.
I even created a new folder called lib, pasted libfwlib32.so.1.0.1 into it and ran
$ ln -s ~/lib/libfwlib32.so.1.0.1 ~/lib/libfwlib32.so
on the console to create a new .so file and gave ~/lib as argument to -L option on the command line. It made no difference. I am at the point of tearing my hair out so any help will be appreciated.
Thanks alot!
You should put -l option in the very last as:
$ g++ minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp -L/usr/local/lib/ -o adapter -lfwlib32 -lpthread
Note: Please make sure that all the header and source file are in the same folder.
Note that specifying -L~/lib won't work as the ~ will not be expanded by the shell. Also you can't add a space between -L and ~/lib. Instead you must specify it as a relative or absolute path.
Have you checked that the libfwlib32.so symlink exists in /usr/local/lib (or /usr/lib) in addition to the libfwlib32.so.1.0.1 file?
Another possibility is that the library is the wrong architecture (ie. 32-bit while your system is 64-bit), but then ld should print a message about skipping incompatible library. You can check the architecture of the library by running 'file libfwlib32.so.1.0.1'.
The error message suggests that -lfwlib32 is being interpreted as a filename not as a -l parameter. Put all the parameters before the files to be compiled
g++ -m32 -L/usr/local/lib/ -lfwlib32 -lpthread -o adapter minIni.c device_datum.cpp fanuc_axis.cpp fanuc_path.cpp service.cpp condition.cpp cutting_tool.cpp string_buffer.cpp logger.cpp client.cpp server.cpp adapter.cpp fanuc_adapter.cpp FanucAdapter.cpp
As has been pointed out by #Erik Johannessen, libfwlib32.so is a 32bit library, so you need to add -m32 to build a 32bit executable.

Error: undefined reference to `sqlite3_open'

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

Compiling parts of Festival code written in C++ in a stand-alone C++ program

I am trying to use selective parts of the Festival code (written in C++) and trying to use them in my own C++ programs. Note that this question is not about using the Festival API but about functions within Festival that can be used directly.
The program I wrote takes in a C++ style string and tries to initialize an object of type EST_String (an internal implementation of the String class in Festival). I then try to print the object.
The code I have:
/*EST_String is a string implementation for the festival project.
* This program simply takes in a C++-style string and returns an EST_String.
* This program is being used to test for makefiles etc.
*/
#include <iostream>
#include <EST_String.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
if(argc != 2) {
cout << "Correct usage: <binary> <string>" << endl;
exit(5);
}
string word(argv[1]);
EST_String e(word.c_str()); //init EST_String.
cout << "C++ String = " << word << endl;
cout << "EST_String = ";
cout << e;
return 0;
}
I am trying to compile it (from the command line directly (no makefile at present)) like so:
g++ -I../../speech_tools/include -I../../speech_tools/base_class/string -L../../speech_tools/lib/ -lestbase -lncurses -lasound -leststring -lestools usingESTString.C -o usingESTString
The error I get is:
/tmp/cczyGTfm.o: In function `main':
usingESTString.C:(.text+0x91): undefined reference to `EST_String::EST_String(char const*)'
/tmp/cczyGTfm.o: In function `EST_Chunk::operator--()':
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x3e): undefined reference to `EST_Chunk::~EST_Chunk()'
usingESTString.C:(.text._ZN9EST_ChunkmmEv[EST_Chunk::operator--()]+0x49): undefined reference to `EST_Chunk::operator delete(void*)'
collect2: ld returned 1 exit status
How can I get the code to compile properly? Where am I going wrong?
Try putting the libraries you link with last on the line.
The linker often resolves references kind of "backwards", meaning that the order of files presented on the command line is important: It wants files containing references first, then the libraries containing those references.
Try adding this to end of your g++ link command: -I/usr/include/festival
-I/usr/include/speech_tools -I/usr/include/boost -lFestival -lestools
-lestbase -leststring
Making sure that the festival and speech_tools headers directory lives at: /usr/include
cd /usr/include
ls festival
ls speech_tools
I am trying to rebuild cogita with festival support, and my program linked successfully after compiling the object files using this line
g++ -Wall -fPIC -Wno-variadic-macros -fopenmp -std=gnu++0x -O2 -g -fstack-protector cogitaconfig.o go-irc.o irc.o whirr-sockets.o -o cogIRCProgram
-rdynamic /usr/local/lib/libcogutil.so -Wl,-rpath,/usr/local/lib
-I/usr/include/festival -I/usr/include/speech_tools -I/usr/include/boost
-lFestival -lestools -lestbase -leststring
I've been trying to link to festival's API too, and the Makefile I wrote executes the following link command
g++ -lpthread build/fetch/festival/src/lib/libFestival.a build/fetch/speech_tools/lib/libestools.a build/fetch/speech_tools/lib/libestbase.a build/fetch/speech_tools/lib/libeststring.a -lcurses -ldl -lm -lstdc++ build/test/speaker.o build/common/message-queue.o build/speaker/speaker-public.o build/fetch/festival/src/lib/libFestival.a -o build/bin/speaker-test
and I get a huge (25k lines) linker error full of undefined references (a part of which is here: http://pastebin.com/PCyV8xAH). I can assert that the *.a files exist (though I'm not sure if they've been built correctly or not). I compile speech_tools with make -j7 and festival with make.
Any suggestions?
I'm running Debian wheezy.

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