C++ - Undefined reference to "sodium_init" - c++

I am attempting to make a testing application using libsodium, however I am getting the error:
main.cpp:6: undefined reference to `sodium_init'
I ran the following commands to install in as the root user.
$ ./configure
$ make && make check
$ make install
This the code that is having the issue.
#include <stdio.h>
#include <sodium.h>
int main(int argc, char **argv)
{
if (sodium_init() == -1)
{
return 1;
}
printf("libsodium had no issues!\n");
return 0;
}
I am using CodeLite as my IDE, and my C++ compiler options are the following:
-g;-O0;-Wall;-lsodium
The options were default and I added -lsodium to the list.
Attempting to compile main.cpp directly from the terminal with the following command g++ -lsodium main.cpp throws the same error.
Could someone please help me with my issue.

Libraries for linking are searched in order, so you need to place the libraries after your local translation units:
g++ main.cpp -lsodium
In your IDE, make sure you add -lsodium as a linker argument.

Related

OpenCV: Undefined reference to xcb_poll_for_reply

As of late I have been getting the following error whenever I try to compile any program that uses the open cv libraries, I use g++ to compile:
g++ Example.cpp -o Ex `pkg-config opencv --cflags --libs`
No matter the content of the file (I have checked with programs that worked a couple of weeks ago) I always get the following error:
/usr/lib64/libX11.so.6: undefined reference to `xcb_poll_for_reply64'
/usr/lib64/libX11.so.6: undefined reference to `xcb_wait_for_reply64'
Do you have any idea of what might be the cause? (and how to fix it)
An example program that fails to compile:
#include "path/opencv2/highgui/highgui.hpp"
#include "path/opencv/highgui.h"
using namespace cv;
int main (int argc, char * argv[])
{
Mat image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE) ;
return 0;
}
Add -lxcb to your command line (this will instruct the linker linking w/ the xcb library). Please make sure the 64b version of xcb is in the linker path (you can always put it explicitly via the -L switch)
The error was caused by some changes done to the libX11.so.6, talked with the FE machines support and they fixed it.

Linking OpenGL using MinGW on Windows x86_64

I would like to develope an application using C++ and OpenGL for Windows 64bit.
I am using the following Compiler x86_64-w64-mingw32-g++.
The following code snippet (Test.cpp) is sufficient to trigger the error I get:
#include <GL/gl.h>
int main(int argn, char **argv) {
glClear(GL_COLOR_BUFFER_BIT);
}
(I know this code is meaningless, but it is sufficient to trigger the error during linking.)
I use the following Makefile:
Test:
g++ -lopengl32 -o Test Test.cpp
This yields the following error:
undefined reference to `__imp_glClear'
I have no clue what I am missing, and would be very thankfull for any advice.
For me the g++ main.cpp -o run.exe -lopengl32 seems to work just fine, so you most likely wont need -Wl,--enable-stdcall-fixup to compile it.

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

Linking programs to C++ Project in Eclipse? Linux

I am given the command to link programs:
g++ -o prog_name prog_name.cc -lsicklms-x -pthread
However, I'm not using the command line, I'm using eclipse.
My Project is called "Test" and I have only 1 class "main.cpp" that is this:
/*
* Test.cpp
*
* Created on: Dec 1, 2011
* Author: igvc
*/
#include <iostream>
#include <sicklms-1.0/SickLMS.hh>
using namespace std;
using namespace SickToolbox;
int main(int argc, char *argv[]) {
/* Specify device path and baud */
string dev_path = "/dev/ttyUSB0";
sick_lms_baud_t lms_baud = SickLMS::SICK_BAUD_38400;
/* Define buffers for return values */
unsigned int measurements[SickLMS::SICK_MAX_NUM_MEASUREMENTS] = {0};
unsigned int num_measurements = 0;
/* Instantiate the object */
SickLMS sick_lms(dev_path);
try {
/* Initialize the device */
sick_lms.Initialize(lms_baud);
/* Grab some measurements */
for(unsigned int i = 0; i < 10; i++) {
sick_lms.GetSickScan(measurements,num_measurements);
cout << "\t" << num_measurements << endl;
}
/* Uninitialize the device */
sick_lms.Uninitialize();
}
catch(...) {
cerr << "error" << endl;
return -1;
}
return 0;
}
I'm getting all these errors:
undefined reference to `SickToolbox::SickLMS::~SickLMS()`
undefined reference to `SickToolbox::SickLMS::GetSickScan(unsigned int*, unsigned int&, unsigned int*, unsigned int*, unsigned int*, unsigned int*, unsigned int*)'
undefined reference to `SickToolbox::SickLMS::Initialize(SickToolbox::SickLMS::sick_lms_baud_t)'
undefined reference to `SickToolbox::SickLMS::SickLMS(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
undefined reference to `SickToolbox::SickLMS::Uninitialize()'
So I think it's a linking problem. And I have that above command to link, which the tutorial says is required, but I'm not sure how to set it up in eclipse.
Where the code says #include sicklms-1.0/SickLMS.hh, I did go to Project>Properties>C/C++ Build>Settings>GCC C++ Compiler>Directories and added the directory to the .h file and that resolved that problem, but now I'm getting those errors. I went to GCC C++ Linker>Libraries>Library search path(-L) and added the sicktoolbox-1.0.1 directory, but i don't know what to put in the Libraries(-l) part.
Thanks for any help, I'm really stuck on this.
Presumably you are using Eclipse CDT?
Right-click on your project and select Properties from the context menu.
In the Properties dialog, select C/C++ Build->Settings->GCC C++ Linker->Libraries
In the Libraries box, click on the + button to add a new library, type sicklms
Your library must be in the linker's path. /usr/lib and /usr/local/lib are fairly standard. The library name must be libsicklms.a or libsicklms.so for -l as the linker prepends lib to the library name.
If sicklms was installed from source with ./configure && make && make install then the library should be in the standard library path i.e. either /usr/lib or /usr/local/lib otherwise, your problem is not your linker, but the way sicklms was installed.
If any of the above instructions are not doable, you need to let me know
a) Version of Eclipse (Galileo, Indigo, etc)
b) If you have CDT installed.
c) How sicklms was installed on your system (package manager, built from source, etc).
Based entirely on what you've written here, I think in the -l part you need to just add -lpthread -lsicklms-x. But that's assuming Eclipse provides enough smarts to replace a tool such as make(1), which is really fantastic for rebuilding large projects.
Try creating a Makefile in your project directory like this:
LDFLAGS=-L/path/to/sicklms-directory -lsicklms-x -lpthread
Running make then gives:
$ make main
g++ -L/path/to/sicklms-directory -lsicklms-x -lpthread main.cpp -o main
/usr/bin/ld.bfd.real: cannot find -lsicklms-x
collect2: ld returned 1 exit status
make: *** [main] Error 1
(It fails because I don't have your libraries available. But it does show the command line that was executed to try to build the source.)
You can probably make some easy changes for compiling Java programs too. I've only got super-simplistic Java sources easily available, but the following Makefile lets me re-build them very easily:
$ cat Makefile
.SUFFIXES: .java .class
.java.class:
javac $<
all: Echo.class Test.class Compress.class
$ make Echo.class Test.class
javac Echo.java
make: `Test.class' is up to date.
$ rm *class
$ make
javac Echo.java
javac Test.java
javac Compress.java
(Note that the indented line is indented with a tab. It must be a tab. make(1) is mighty archaic in some seriously inconvenient ways.)

Trouble with libstatgrab

Having trouble using libstatgrab -- I receive the following error at compile time:
"libstatgrabTest.cpp:16: undefined reference to sg_get_process_stats"
I'm guessing it is because I need to include it's .so files at the linking state -- although I'm not sure. As you can see below, I am currently including the statgrab.h header file. I performed configured && make && make install for the library.
If I search for libstatgrab*, I come across the following:
./usr/local/lib/libstatgrab.so.6.2.3
./usr/local/lib/libstatgrab.la
./usr/local/lib/libstatgrab.so.6
./usr/local/lib/libstatgrab.a
./usr/local/lib/libstatgrab.so.6.2.2
./usr/local/lib/libstatgrab.so
./usr/local/lib/pkgconfig/libstatgrab.pc
Another search for statgrab* returns the following (relevant items only):
./usr/local/bin/statgrab
./usr/local/include/statgrab_deprecated.h
./usr/local/include/statgrab.h
At compilation, I run: g++ -g -c libstatgrabTest.cpp
At linking, I run: g++ -L/usr/local/lib libstatgrab.o -o libstatgrabTest
Any idea what I am doing wrong? The code within libstagrabTest.cpp is shown below:
// external libraries
#include <statgrab.h> // libstatgrab (http://www.i-scream.org/libstatgrab/)
// namespace
using namespace std;
int main(void) {
// try to initalize libstatgrab
int * entries;
sg_process_stats * systemStats = sg_get_process_stats(entries);
// return
return 0;
}
g++ -L/usr/local/lib libstatgrab.o -o libstatgrabTest
should be (tested with your code snipper on Ubuntu Natty):
g++ -L/usr/local/lib -o libstatgrabTest -lstatgrab
or, to link statically:
g++ -L/usr/local/lib -o libstatgrabTest /usr/lib/libstatgrab.a
Tested both on my box, with your exact source.