Cannot link with libiberty - c++

I am trying to call cplus_demangle() function from libiberty.a, but I am getting "undefined reference to 'cplus_demangle' error.
This is my simple main:
extern "C" char *cplus_demangle(const char *, int);
int main() {
cplus_demangle("a", 0);
}
and my build command (libiberty is in ../../install/lib64):
g++ -L../../install/lib64 -liberty main.cpp -o main
If I rename libiberty.a to something else I'd get "cannot find -liberty" error. So, I assume the linker sees the library. And I can see cplus_demangle in libiberty.a:
$> nm -C --defined-only libiberty.a | grep "cplus_demangle"
0000000000002230 T cplus_demangle
...
But I am getting this error when I compile:
main.cpp:(.text+0xf): undefined reference to `cplus_demangle'
collect2: ld returned 1 exit status
make: *** [main] Error 1
I'd appreciate your help. Thanks!

Move the -liberty to the end of the command line. The library should be searched after main.o is created. Order matters.

Related

Library connection in GCC

I have 4 files:
main.cpp
emcd.h
EMCB.dll
EMCB.lib
I want to use the functions that are in the dll.
There is not a lot of code, but it captures the essence.
#pragma comment(lib,"EMCB.lib")
#include "emcb.h"
int main()
{
EMCBLibInitialize(); //DLL function
}
To compile, I use gcc and do it with the following command
gcc main.cpp -l: EMCB.lib
after which I get an error
/usr/bin/ld: cannot find -l:EMCB.lib
collect2: error: ld returned 1 exit status
The preprocessor statement:
#pragma comment(lib,"xxx.lib")
will not be recognized by the GCC compiler. You need to use -l

How to link libssh library in Makefile

I get following error, when i tried to compile my test program
g++ main.cpp -o main
/tmp/ccICBwKO.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `ssh_new'
main.cpp:(.text+0x2d): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
In my make file i have:
all: main
$main: main.cpp
g++ main.cpp -o main -lssh
Thanks for help. (I really hate C/C++...)
As you can see from the log, make is not using the rule you have given for $main. I suspect that you want to name it either main or $(main).
$main concatenates the value of variable $m (defaults to nothing as m is not defined) with ain, so that you've created a rule for ain instead of main. make thus rely on its default rules for creating an executable from a .cpp file, which does not include linking with libssh.

Undefined reference to __dso_handle_ - compiling C++ on cygwin

I have a basic Hello World C++ program that I am trying to compile and run on cygwin with g++. The code is:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world." << endl;
return 0;
}
Compiling it with: g++ helloWorld.cpp -o h results in the following error:
/tmp/ccDO1S4J.o:helloWorld.cpp:(.rdata$.refptr.__dso_handle[.refptr.__dso_handle]+0x0): undefined reference to__dso_handle'
collect2: error: ld returned 1 exit status`
I have been reading up on some other threads that indicate that it might be a linker problem and that invoking the linker separately or with verbose output might lead to some more clues.
So, I did:
1. g++ -c helloWorld.cpp -o helloWorld.o (this works - no errors).
2. ld -o h helloWorld.o causes a lot of undefined reference to __main or std::cout etc. errors.
I think this is a linking issue and that I need to link another library perhaps. Any pointers on how to solve this are most welcome.
Re-installing g++ via the installer application on cygwin worked.

#including <iostream> breaks linkage of shared object

I have following code:
#include <iostream>
extern "C" {
void foo() {
std::cout << "Wow, It's working!" << std::endl;
}
}
Without inclusion of iostream and printing the library links correctly. But when I include and try to print, compiler gives me following errors:
Compiling: main.cpp
Linking dynamic library: libfoo.so
/usr/bin/ld: obj/main.o: перемещение R_X86_64_32 для «.rodata» не может использоваться при создании общего объекта; перекомпилируйте с -fPIC
obj/main.o: could not read symbols: Некорректное значение
collect2: ошибка: выполнение ld завершилось с кодом возврата 1
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
Sorry for the errors in Russian, but I have no exact translation into English. Maybe this translation will help:
Compiling: main.cpp
Linking dynamic library: libfoo.so
/usr/bin/ld: obj/main.o: relocation R_X86_64_32 against ".rodata" can not be used when making shared object; recompile with -fPIC
obj/main.o: could not read symbols: bad value
collect2: error: ld terminated with exit code 1
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
How I must include and compile to get it working?
P.S.
The grammar can be very bad: English is not my native language
Have you tried -lstdc++ linker switch? How does your makefile look like?

Compiling/linking to libpano13

I am trying to get a very simple program to compile while using functions from libpano13, but I am running into linking errors. I am on Ubuntu 10.04, and have done the following:
sudo apt-get install libpano13-dev
My sample program is as follows:
#include <cstring>
#include <pano13/PTcommon.h>
int main(int argc, char **argv) {
fullPath *outFile = new fullPath();
StringtoFullPath(outFile, (char *)"/tmp/randomImage.jpg");
return 0;
}
I am attempting to compile this with the following command (the above code is located in a file called panoTest.cpp):
$ g++ panoTest.cpp -o testApp -L/usr/lib -lpano13
When I run this command I get the following error:
$ g++ panoTest.cpp -o testApp -L/usr/lib -lpano13
/tmp/ccyIioEi.o: In function `main':
panoTest.cpp:(.text+0x87): undefined reference to `StringtoFullPath(fullPath*, char*)'
collect2: ld returned 1 exit status
make: *** [all] Error 1
Now, I've checked /usr/lib for libpano* and found the following files:
$ ls /usr/lib/libpano*
/usr/lib/libpano13.a /usr/lib/libpano13.so /usr/lib/libpano13.so.1
/usr/lib/libpano13.so.1.0.0
I've checked libpano13.so using nm and got the following output:
$ nm --demangle /usr/lib/libpano13.a | grep StringtoFullPath
U StringtoFullPath
U StringtoFullPath
00000000 T StringtoFullPath
$ nm --demangle /usr/lib/libpano13.so | grep StringtoFullPath
nm: /usr/lib/libpano13.so: no symbols
I also checked it using objdump:
$ objdump -T /usr/lib/libpano13.so | grep StringtoFullPath
00057fd0 g DF .text 0000004c Base StringtoFullPath
So, the following seems to be true:
libpano13 exists in /usr/lib,
libpano13 contains the symbols for StringtoFullPath,
I am using the correct library directory of /usr/lib,
I am using the correct library name of pano13, and
g++ is able to locate the library
But, for some reason, g++ is unable to find the symbol for StringtoFullPath.
Does anyone have any suggestions/ideas about why this is happening?
Any help would be greatly appreciated!
The
undefined reference to `StringtoFullPath(fullPath*, char*)'
worries me a bit: it contains the types of the parameters. Most Linux libraries are not C++, so there should be no types...
Try surrounding the #include for the header with an extern "C" { ... } block.