Can't link a C shared object to a C++ program - c++

I am trying to write a makefile for a project. The project involves a test program that defines a main function written in C++11 that is supposed to call a shared object library written in c99 and run some tests.
My makefile successfully compiles the c99 library and produces "libhanoi.so".
When I try to link the C99 library to the C++11 part, I get the following error:
g++ -std=gnu++11 -L. -lhanoi -o main tests/testing.cpp tests/main.cpp
/tmp/cctHQTcW.o: In function `binarion_constructor(unsigned long*)':
main.cpp:(.text+0x28): undefined reference to `binarion64_t'
collect2: error: ld returned 1 exit status
Makefile:29: recipe for target 'tests' failed
make: *** [tests] Error 1
However, the output of "nm -C libhanoi.so" shows that the binarion64_t function is being exported by libhanoi.so:
0000000000000610 T binarion64_t(long long, long long)
When I introduce a typo into the name of libhanoi.so, it introduces an error saying it can't find libhanoi.so.
So it must be able to find libhanoi.so and libhanoi.so is exporting the unimplemented function in main.cpp, yet it still is giving an undefined reference. What's going on?
Minimal example:
hanoi.h:
#ifndef HANOI_H
#define HANOI_H
#include <inttypes.h>
// binarion (base -1+i) constructor
uint64_t binarion64_t(long long A, long long B);
#endif // HANOI_H
binarion.c:
#include "hanoi.h"
uint64_t binarion64_t(long long A,long long B){
return 0;
}
main.cpp:
#include <stdio.h>
extern "C" {
#include "hanoi.h"
};
uint64_t binarion_constructor(uint64_t * args){
return binarion64_t(args[0], args[1]);
}
int main(void){
return 0;
}
Compile:
g++ -std=c99 -c binarion.c
g++ -std=c99 -shared -o libhanoi.so binarion.o -lm
g++ -std=gnu++11 -L. -lhanoi -o main main.cpp
output:
/tmp/ccjoRmCg.o: In function `binarion_constructor(unsigned long*)':
main.cpp:(.text+0x28): undefined reference to `binarion64_t'
collect2: error: ld returned 1 exit status
EDIT:
The commands I'm running are:
gcc -std=c99 -c binarion.c
gcc -std=c99 -shared -o libhanoi.so binarion.o -lm
g++ -std=gnu++11 -L. -lhanoi -o main main.cpp
The files are exactly the ones in the question. The output of "readelf -s libhanoi.so | grep binarion" is:
12: 0000000000000660 19 FUNC GLOBAL DEFAULT 11 binarion64_t
33: 0000000000000000 0 FILE LOCAL DEFAULT ABS binarion.c
46: 0000000000000660 19 FUNC GLOBAL DEFAULT 11 binarion64_t
and the output of "g++ -std=gnu++11 -L. -lhanoi -o main main.cpp" is:
/tmp/cczfgY8M.o: In function `binarion_constructor(unsigned long*)':
main.cpp:(.text+0x28): undefined reference to `binarion64_t'
collect2: error: ld returned 1 exit status

TL; DR:
Use:
gcc -std=c99 -c binarion.c
gcc -std=c99 -shared -o libhanoi.so binarion.o -lm
g++ -std=gnu++11 -L. -lhanoi -o main main.cpp
Explanation:
You should be using gcc to compile a C file, g++ is for C++. When you do g++ -std=c99 -c binarion.c the compiler gives you a hint with:
cc1plus: warning: command line option ‘-std=c99’ is valid for C/ObjC but not for C++
This means that you end up compiling your library as a C++ library. You can verify that by calling readelf -s libhanoi.so | grep binarion:
9: 00000000000005da 19 FUNC GLOBAL DEFAULT 9 _Z12binarion64_txx
29: 0000000000000000 0 FILE LOCAL DEFAULT ABS binarion.c
44: 00000000000005da 19 FUNC GLOBAL DEFAULT 9 _Z12binarion64_txx
As you can see, the function has been name-mangled, which is something C++ does, and C doesn't.
However, when compiling main.cpp you tell the compiler that binarion_t has C linkage:
extern "C" {
#include "hanoi.h"
};
So it is searching for unmangled binarion_t (instead of _Z12binarion64_txx).

Related

g++ ignores inline keyword (odr) unless diagnostic, optimizer or standard flags are given?

I have these three files:
test.h
inline int foo(int i) {
return i;
}
asdf1.cpp
#include "test.h"
int main () {
}
asdf2.cpp
#include "test.h"
int bar () {
return 42;
}
When compiling with g++ asdf1.cpp asdf2.cpp, this happens:
$ g++ asdf1.cpp asdf2.cpp
/usr/bin/ld: /tmp/ccTPkxPK.o: in function `foo(int)':
asdf2.cpp:(.text+0x0): multiple definition of `foo(int)'; /tmp/ccJLbLeL.o:asdf1.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
$ g++ asdf1.cpp asdf2.cpp -O1
$ g++ asdf1.cpp asdf2.cpp -Wpedantic
$ g++ asdf1.cpp asdf2.cpp -std=c++14
$ # all of the above work
Why does g++ ignore the inline keyword unless some unrelated flag is given?
Especially weird since, with the -v flag, g++ tells me that it's using c++14 even when nothing is specified. Also, as far as I'm aware, the inline keyword (in this usage) exists in every C++ standard.

Undefined reference (but nm says the function exists) [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I have a library from a camera manufacturer, and in the demo code provided, a function called metadata_init() works fine, in my code though, I get an undefined reference error.
The make output from the demo code:
/opt/linaro-multilib-2013.09-gcc4.8/bin/arm-linux-gnueabihf-gcc -lm -g -L"/home/aro/Downloads" -o hicore demoS2.c -lpthread -lyuvlib -lrt
and thats it, the build succeeds fine, I will have a working ./hicore application in there.
My project is a little bit more complicated, and I compile using eclipse.
The console output is:
11:33:23 **** Build of configuration Camera-R4-Debug for project Camera ****
make all
Building file: ../src/Camera.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Camera.d" -MT"src/Camera.o" -o "src/Camera.o" "../src/Camera.cpp"
../src/Camera.cpp: In function ‘int metadata_construct_http_message(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*)’:
../src/Camera.cpp:379:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
"</svg>\r\n";
^
../src/Camera.cpp: In function ‘int main()’:
../src/Camera.cpp:426:97: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
(void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
^
Finished building: ../src/Camera.cpp
Building file: ../src/CameraFrameGrabber.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraFrameGrabber.d" -MT"src/CameraFrameGrabber.o" -o "src/CameraFrameGrabber.o" "../src/CameraFrameGrabber.cpp"
Finished building: ../src/CameraFrameGrabber.cpp
Building file: ../src/CameraLogger.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraLogger.d" -MT"src/CameraLogger.o" -o "src/CameraLogger.o" "../src/CameraLogger.cpp"
Finished building: ../src/CameraLogger.cpp
Building file: ../src/CameraParameter.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraParameter.d" -MT"src/CameraParameter.o" -o "src/CameraParameter.o" "../src/CameraParameter.cpp"
Finished building: ../src/CameraParameter.cpp
Building target: Camera
Invoking: Cross G++ Linker
arm-linux-gnueabihf-g++ -L/opt/Camerasdk/R4/lib -L"/cameraBuilds/Wrapper/Camera-R4-Debug" -L"/opt/ExternalLibraries/curl/Camera/R4/lib" -L"/opt/ExternalLibraries/libxml2/Camera/R4/lib" -L"/opt/ExternalLibraries/OpenCV24/Camera/R4/lib" -L"/opt/DetectionModules/Camera/R4/lib" -L/home/aro/Downloads -o "Camera" ./src/Camera.o ./src/CameraFrameGrabber.o ./src/CameraLogger.o ./src/CameraParameter.o -lWrapper -lxml2 -lopencv_highgui -lopencv_imgproc -lopencv_core -lpthread -lyuvlib -lrt -llibjasper -llibjpeg -llibpng -llibtiff -lzlib -lcurl
./src/Camera.o: In function `main':
/home/aro/cameraBuilds/Camera/Camera-R4-Debug/../src/Camera.cpp:426: undefined reference to `metadata_init(char*, int, int (*)(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*))'
collect2: error: ld returned 1 exit status
make: *** [Camera] Error 1
11:33:24 Build Finished (took 1s.212ms)
The code itself is the very same thing, I just copied it over.
Metadata.h:
#ifndef _HIK_METADATA_H_
#define _HIK_METADATA_H_
const int max_http_body_len = 100 * 1024;
typedef enum
{
CMD_ADD_TYPE = 1,
CMD_OTHER,
} METADATA_CTRL_TYPE;
typedef struct
{
int length;
int fd;
METADATA_CTRL_TYPE cmd_type;
} METADATA_HEADER;
typedef struct
{
char option[128];
int share_socket;
} METADATA_ADD_CFG;
typedef enum
{
HTTP_HEADER_TYPE = 1,
HTTP_BODY_TYPE,
} METADATA_HTTP_MESSAGE_TYPE;
typedef struct
{
char boundary[64];
char http_content_type[64];
char multipart_content_type[64];
} METADATA_MULTIPART_TYPE;
typedef int (*p_metadata_construct_http_msg_callback_f)(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len);
int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);
#endif
In Camera.cpp:
#include "Metadata.h"
int metadata_construct_http_message(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len)
{
// Removed for SO
return 0;
}
int main()
{
(void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
...
}
What causes this, and how can I debug this issue to narrow down how to fix it?
It think the problem could be that metadata_init is a C function, but you use it from a C++ code.
In case of this, extern "C" must be used in the header file, like this:
#ifdef __cplusplus
extern "C" {
#endif
// embed the whole contents of the header file here, I just put the function here for brevity
int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);
#ifdef __cplusplus
}
#endif
It is because of name mangling rules. Names are mangled differently in C and C++. With extern "C", you tell the compiler that the names inside should be used with "C" mangling.
C++ has to do a complex name mangling compared to C, because it has to embed almost all signature information to a name (all parameters type), while in C, the mangled name usually is the same as the function name, or there is a _ prepended.

undefined reference in linking against C++ library

I write a C++ library and when linking against the library the symbols in it cannot be found. Here's what I've got:
a.cpp:
void zak()
{
}
test.cpp:
extern void zak();
int main(int argc, const char ** argv)
{
zak();
}
Makefile:
all:
g++ -c -o a.o a.cpp
ar r libzak.a a.o
g++ -L. -lzak test.cpp -o test
Here is what make says on my (Linux Mint 13) box:
g++ -c -o a.o a.cpp
ar r libzak.a a.o
g++ -L. -lzak test.cpp -o test
/tmp/ccC4cnLV.o: In function `main':
test.cpp:(.text+0x7): undefined reference to `zak()'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
I am sure I am missing something obvious, but what is it?
Link order matters. Put -lzak after test.cpp on the link line.
I think that -l is for shared libraries (.so). Try this: g++ libzak.a test.cpp -o test

Static Libraries which depend on other static libraries

I have a question about making static libraries that use other static libraries.
I set up an example with 3 files - main.cpp, slib1.cpp and slib2.cpp. slib1.cpp and slib2.cpp are both compiled as individual static libraries (e.g. I end up with slib1.a and slib2.a) main.cpp is compiled into a standard ELF executable linked against both libraries.
There also exists a header file named main.h which prototypes the functions in slib1 and slib2.
main.cpp calls a function called lib2func() from slib2. This function in turn calls lib1func() from slib1.
If I compile the code as is, g++ will return with a linker error stating that it could not find lib1func() in slib1. However, if I make a call to lib1func() BEFORE any calls to any functions in slib2, the code compiles and works correctly.
My question is simply as follows: is it possible to create a static library that depends on another static library? It would seem like a very severe limitation if this were not possible.
The source code for this problem is attached below:
main.h:
#ifndef MAIN_H
#define MAIN_H
int lib1func();
int lib2func();
#endif
slib1.cpp:
#include "main.h"
int lib1func() {
return 1;
}
slib2.cpp:
#include "main.h"
int lib2func() {
return lib1func();
}
main.cpp:
#include <iostream>
#include "main.h"
int main(int argc, char **argv) {
//lib1func(); // Uncomment and compile will succeed. WHY??
cout << "Ans: " << lib2func() << endl;
return 0;
}
gcc output (with line commented out):
g++ -o src/slib1.o -c src/slib1.cpp
ar rc libslib1.a src/slib1.o
ranlib libslib1.a
g++ -o src/slib2.o -c src/slib2.cpp
ar rc libslib2.a src/slib2.o
ranlib libslib2.a
g++ -o src/main.o -c src/main.cpp
g++ -o main src/main.o -L. -lslib1 -lslib2
./libslib2.a(slib2.o): In function `lib2func()':
slib2.cpp:(.text+0x5): undefined reference to `lib1func()'
collect2: ld returned 1 exit status
gcc output (with line uncommented)
g++ -o src/slib1.o -c src/slib1.cpp
ar rc libslib1.a src/slib1.o
ranlib libslib1.a
g++ -o src/slib2.o -c src/slib2.cpp
ar rc libslib2.a src/slib2.o
ranlib libslib2.a
g++ -o src/main.o -c src/main.cpp
g++ -o main src/main.o -L. -lslib1 -lslib2
$ ./main
Ans: 1
Please, try g++ -o main src/main.o -L. -Wl,--start-group -lslib1 -lslib2 -Wl,--end-group.
Group defined with --start-group, --end-group helps to resolve circular dependencies between libraries.
See also: GCC: what are the --start-group and --end-group command line options?
The order make the difference. Here's from gcc(1) manual page:
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.

Undefined reference to typeinfo when linking with nsRunnable

As firefox 12 sdk removed the 'Proxy' object, I wanted to use cross-thread calls using nsRunnable. I basically did copy-and paste of the code:
class NotifyTask : public nsRunnable
{
public:
NotifyTask(nsISupports *subject, const char *topic, bool remref)
: mWorkerThread(do_GetCurrentThread())
{
MOZ_ASSERT(!NS_IsMainThread()); // This should be running on the worker thread
}
NS_IMETHOD Run() {
MOZ_ASSERT(NS_IsMainThread()); // This method is supposed to run on the main thread!
mWorkerThread->Shutdown();
return NS_OK;
}
private:
nsCOMPtr<nsIThread> mWorkerThread;
};
And I tried to compile it. I got following errors:
g++ -std=gnu++0x -Wall -O2 -c -DUSE_LIBUSB -fPIC -DHAVE_CRYPTO -fpermissive -DCRYPTPP -fshort-wchar -I../../xulrunner-sdk/include -o gipsy.o gipsy.cpp
gipsy.cpp:74:7: warning: ‘NotifyTask’ declared with greater visibility than the type of its field ‘NotifyTask::<anonymous>’ [-Wattributes]
gipsy.cpp:74:7: warning: ‘NotifyTask’ declared with greater visibility than its base ‘nsRunnable’ [-Wattributes]
g++ -std=gnu++0x -Wl,-z,defs -Wall -Os -o gipsy.so -shared gipsy.o gipsymodule.o tracklog.o gpslib/data.o gpslib/garmin.o gpslib/gps.o gpslib/phys.o gpslib/igc.o gpslib/aircotec.o cp1250.o prefparser.o gpslib/foreignigc.o gpslib/mlr.o gpslib/flymaster.o gpslib/compeo.o gpslib/iq.o ../libs/libcryptopp.a -lusb -L../../xulrunner-sdk/lib -lxpcomglue_s -lxul -lxpcom -lplds4 -lplc4 -lnspr4 -lpthread -ldl -lmozalloc
gipsy.o:(.data.rel.ro._ZTI10NotifyTask[typeinfo for NotifyTask]+0x10): undefined reference to `typeinfo for nsRunnable'
collect2: ld returned 1 exit status
According to g++ undefined reference to typeinfo it might be that gecko sdk was built with -fvisibility=hidden and some weird 'key method in different .so'. Is this an error in Gecko sdk or am I doing something wrong?
The gecko SDK is not link with RTTI - adding "-fno-rtti" parameter to the particular .o file fixed the problem.