g++ can't find headers but I did include them - c++

I am starting on c++ and already going wrong ...
I am trying to compile a small test of levelDB :
#include <assert.h>
#include "leveldb/db.h"
using namespace std;
int main() {
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
assert(status.ok());
return 1;
}
Here is the g++ command :
g++ -I include/ testLevelDB.cpp
Output:
/tmp/ccuBnfE7.o: In function `main':
testLevelDB.cpp:(.text+0x14): undefined reference to `leveldb::Options::Options()'
testLevelDB.cpp:(.text+0x57): undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)'
The include folder is the one with the levelDB headers.

You need to tell the linker to link to the leveldb library such as
g++ -I include/ testLevelDB.cpp -lleveldb
But this won't work if the library is not in /usr/lib or /usr/local/lib for that case assuming the libleveldb.so exists in some path called $LEVELDB_PATH you need to do
g++ -I include -L $LEVELDB_PATH testLevelDB.cpp -lleveldb
-L is much like -I but it tells the linker where to looks for libraries.
Also since you seem to be new to gcc world, please have a look at this gcc intro document.

It is a linkage error. Not related to the headers. Did you link with this lib (-l..) ?

Related

How to use CityHash128 in c++ code?

I am trying to use google's cityhash hashing function. I am unable to link it to my c++ code. I have installed cityHash and it has generated libcityhash.la, etc files in my /usr/local/lib.
I am setting LD_LIB_LIBRARY=/usr/local/lib, but it doesn't seem to link to these files.
CODE:
#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
std::ifstream file("dev/urandom");
char buff[4096];
file.read(buff, 4096);
const uint128 hashed = CityHash128(buff,4096);
file.close();
}
Compiling:
g++ -o city cityHash.cpp
Error:
/tmp/cctSoHTX.o: In function main:
cityHash.cpp:(.text+0x73): undefined reference to `CityHash128(char const*, unsigned long)'
collect2: error: ld returned 1 exit status
I include "city.h" and trying to compile it as follows:
g++ -I /usr/local/include/ -L/usr/local/lib -llibcityhash.a cityHash.cpp -o city
But i m still getting :undefined reference to `CityHash128(char const*, unsigned long)' –
Ok, it's the good old "order makes a difference". Instead of:
g++ -I /usr/local/include/ -L/usr/local/lib /usr/local/lib/libcityhash.a cityHash.cpp -o city
you should do:
g++ -I /usr/local/include/ -L/usr/local/lib cityHash.cpp -o city -lcityhash
(libraries and object files are processed in the order of appearance in the command line, and since none of the code so far has used anything from the library when you list it, nothing gets include from that library - then when you get to the actual code that does use it, you don't give the linker the library after it, so it can't find the symbol - note that this is dependant on the behaviour of the linker, so the same rules may not apply in for example a MS Visual Studio compiler/linker setup)

/usr/bin/ld: cannot find -llibboost

Alright So right now I am attempting use the boost C++ libraries in Linux (Ubuntu 12.04) as I have previously used them in Windows. So using some example code from the Boost's site
testfile.cpp
#include <boost/filesystem/convenience.hpp>
#include <boost/foreach.hpp>
#include <boost/range.hpp>
#include <iostream>
int main(int, char**)
{
namespace bf = boost::filesystem;
BOOST_FOREACH(bf::path path,
boost::make_iterator_range(
bf::recursive_directory_iterator(bf::path("/home")),
bf::recursive_directory_iterator())) {
std::cout << path.string() << std::endl;
}
return 0;
}
Should very easily compile using this command
g++ -L/usr/local/lib -o "testfile" -llibboost_filesystem
My problem I am getting the linker error
/usr/bin/ld: cannot find -llibboost_filesystem
and cannot seem to figure out what I am missing. Please Help.
By convention, library names use the lib prefix on most Linux distributions. You should remove this prefix when instructing the linker which libraries to search for. Assuming the gnu ld linker, the documentation says
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to
link. This option may be used any number of times. If namespec is of the
form :filename, ld will search the library path for a file called filename,
otherwise it will search the library path for a file called libnamespec.a.
so you either want
g++ -L/usr/local/lib -o "testfile" -lboost_filesystem
or
g++ -L/usr/local/lib -o "testfile" -l :libboost_filesystem.so

Headers not being recognised

The Background
I have the following source code
#include <libubuntuone-1.0/u1-music-store.h>
#include <libsyncdaemon-1.0/libsyncdaemon/libsyncdaemon.h>
static void
get_credentials (U1MusicStore *music_store,
gchar **oauth_consumer_token,
gchar **oauth_consumer_secret,
gchar **oauth_token,
gchar **oauth_token_secret)
{
SyncdaemonCredentials *credentials;
*oauth_consumer_token = *oauth_consumer_secret = *oauth_token = *oauth_token_secret = NULL;
*oauth_consumer_token = g_strdup (syncdaemon_credentials_get_consumer_key (credentials));
*oauth_consumer_secret = g_strdup (syncdaemon_credentials_get_consumer_secret (credentials));
*oauth_token = g_strdup (syncdaemon_credentials_get_token (credentials));
*oauth_consumer_secret = g_strdup (syncdaemon_credentials_get_token_secret (credentials));
}
int main()
{
return 0;
}
and I am compiling it with the following makefile
main: main.o
g++ main.o -o main
main.o: main.cpp
g++ -c main.cpp `pkg-config --cflags --libs gtk+-2.0`
I need to include the pkg-config option since the u1-music-store.h header tried to included gtk/gtk.h, but the compiler isn't able to find it on it's own.
libsyncdaemon.h is a meta-header whose only purpose is to include a larger list of headers, which can be seen below
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-authentication.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-config-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-credentials.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-daemon.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-events-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-file-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-filesystem-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-folder-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-folders-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-publicfiles-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-share-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-status-info.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-status-interface.h>
#include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-transfer-info.h>
My problem
Whenever I try to compile my code, I get the following error:
main.o: In function `get_credentials(_U1MusicStore*, char**, char**, char**, char**)':
main.cpp:(.text+0x34): undefined reference to `syncdaemon_credentials_get_consumer_key'
main.cpp:(.text+0x3c): undefined reference to `g_strdup'
main.cpp:(.text+0x4e): undefined reference to `syncdaemon_credentials_get_consumer_secret'
main.cpp:(.text+0x56): undefined reference to `g_strdup'
main.cpp:(.text+0x68): undefined reference to `syncdaemon_credentials_get_token'
main.cpp:(.text+0x70): undefined reference to `g_strdup'
main.cpp:(.text+0x82): undefined reference to `syncdaemon_credentials_get_token_secret'
main.cpp:(.text+0x8a): undefined reference to `g_strdup'
collect2: ld returned 1 exit status
make: *** [main] Error
Using grep, I've tracked down the four syncdaemon_credentials_get_* functions to syncdaemon-credentials.h, which I would expect the compiler to be able to find since it's listed in libsyncdaemon.h, but for some reason that's not happening. I'm assuming it's for the reason that u1-music-store.h was unable to find gtk/gtk.h that compelled me to use the pkg-config option in my makefile, but I'm having trouble understanding why this is even the case to begin with. If the file's #included, I would expect it to be included by the compiler.
Using grep again I was able to track down g_strdup to a number of headers, but I also discovered that when I replaced by makefile with the single command
g++ main.cpp -o main `pkg-config --cflags --libs gtk+-2.0`
I can eliminate the g_strdup warning, and I'm just left with the function errors.
My question
There are two things I'm looking to know here:
What should my makefile look like in order to solve my specific problem
What is the general solution to my problem? I'm guessing it's something to do with daisy-chaining #include directives together, and having to use pkg-config to fix that, but I'm not sure.
You need to link against libsyncdaemon.so. Get the appropriate arguments from pkg-config ... libsyncdaemon-1.0.
You need to link against libraries that export the required symbols. If there is a .pc file then you can use it to get the appropriate arguments.
"Unresolved external symbol" (MSVC) and "undefined reference to" (GCC) mean that the compiler found the declarations but the linker couldn't find the definitions.
This either means that you forgot to compile and/or link against a .cpp or that you forgot to link against an external library (.lib (Windows) / .a (Unix/Linux)) or object file (.o) which contains said definitions.

undefined referance to LibSerial

So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++
(I've been working with C recently, and before that java)
Now I need to use LibSerial,
(it seems much simpler to use than C's termios)
my code is:
//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;
int main(int argc, char*argv[])
{
if (argc<2)
{
std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
return 1;
}
SerialStream theSerialStream(argv[1]); //open the device
return 0;
}
When I compile the output:
g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1
This is the linker complaining that it cannot find the functions referenced by the libserial header file.
If I look on my Linux system to see how the shared library is called:
$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0
On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into
g++ -Wall -lserial -o gen1 gen1.cpp string2num.o
Including the header file is not enough - you also need to link with the library that implements SerialStream. Assuming it is a static library called serstream.a (it is almost certainly actually called something else):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a
old thread, but i still use Libserial. here the completed answer
My working setup.
Ubuntu 18.04
g++ 7.3.0
1) Install package for libserial
apt install libserial-dev
2) check for your headers(.h) and .so files
dpkg -l libserial0
dpkg -l libserial-dev
the first command give you the directory of shared library and the second gives you the headers location.
3) Your code.
I have to change a little your code, first i delete the custom header and modifing the constuctor call to this.
SerialStream theSerialStream;
4) compile with g++
Here my compiling command
g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread
check for the -lpthread linking option, beacuse Libserial uses mutex.
In Ubuntu/Debian make sure you have to libserial-dev package installed and use the '-lserial' flag for gcc.

Linux c++ error: undefined reference to 'dlopen'

I work in Linux with C++ (Eclipse), and want to use a library.
Eclipse shows me an error:
undefined reference to 'dlopen'
Do you know a solution?
Here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*desk)(char*);
char *error;
handle = dlopen ("/lib/CEDD_LIB.so.6", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
desk= dlsym(handle, "Apply");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
dlclose(handle);
}
You have to link against libdl, add
-ldl
to your linker options
#Masci is correct, but in case you're using C (and the gcc compiler) take in account that this doesn't work:
gcc -ldl dlopentest.c
But this does:
gcc dlopentest.c -ldl
Took me a bit to figure out...
this doesn't work:
gcc -ldl dlopentest.c
But this does:
gcc dlopentest.c -ldl
That's one annoying "feature" for sure
I was struggling with it when writing heredoc syntax and found some interesting facts. With CC=Clang, this works:
$CC -ldl -x c -o app.exe - << EOF
#include <dlfcn.h>
#include <stdio.h>
int main(void)
{
if(dlopen("libc.so.6", RTLD_LAZY | RTLD_GLOBAL))
printf("libc.so.6 loading succeeded\n");
else
printf("libc.so.6 loading failed\n");
return 0;
}
EOF
./app.exe
as well as all of these:
$CC -ldl -x c -o app.exe - << EOF
$CC -x c -ldl -o app.exe - << EOF
$CC -x c -o app.exe -ldl - << EOF
$CC -x c -o app.exe - -ldl << EOF
However, with CC=gcc, only the last variant works; -ldl after - (the stdin argument symbol).
I was using CMake to compile my project and I've found the same problem.
The solution described here works like a charm, simply add ${CMAKE_DL_LIBS} to the target_link_libraries() call
The topic is quite old, yet I struggled with the same issue today while compiling cegui 0.7.1 (openVibe prerequisite).
What worked for me was to set: LDFLAGS="-Wl,--no-as-needed"
in the Makefile.
I've also tried -ldl for LDFLAGS but to no avail.
you can try to add this
LIBS=-ldl CFLAGS=-fno-strict-aliasing
to the configure options
You needed to do something like this for the makefile:
LDFLAGS='-ldl'
make install
That'll pass the linker flags from make through to the linker. Doesn't matter that the makefile was autogenerated.
I met the same problem even using -ldl.
Besides this option, source files need to be placed before libraries, see undefined reference to `dlopen'.
In order to use dl functions you need to use the -ldl flag for the linker.
how you do it in eclipse ?
Press Project --> Properties --> C/C++ build --> Settings --> GCC C++ Linker --> Libraries -->
in the "Libraries(-l)" box press the "+" sign --> write "dl" (without the quotes)-> press ok --> clean & rebuild your project.
$gcc -o program program.c -l <library_to_resolve_program.c's_unresolved_symbols>
A good description of why the placement of -l dl matters
But there's also a pretty succinct explanation in the docs
From $man gcc
-llibrary
-l library
Search the library named library when linking. (The second
alternative with the library as a separate argument is only for POSIX
compliance and is not recommended.)
It makes a difference where in the command you write this option; the
linker searches and processes libraries and object files in the order
they are specified. Thus, foo.o -lz bar.o searches library z after
file foo.o but before bar.o. If bar.o refers to functions in z,
those functions may not be loaded.
Try to rebuild openssl (if you are linking with it) with flag no-threads.
Then try to link like this:
target_link_libraries(${project_name} dl pthread crypt m ${CMAKE_DL_LIBS})
In earlier versions(~2.7) of GNU tool chain, glibc did not have direct interface to link loader(dlopen and dlsym functions), so you had to provide -ldl(libdl) at compile time. You don't have to do that anymore with latest glibc version. Just include <dlcfn.h> and you are good to go.