Link .o (C object file) to C++ file/project on Linux - c++

I have a problem with .o file in linux as follow:
I have Visual.cpp which call function ECL_Drawrect() and this function is defined as:
#define ECL_Drawrect ECL_bDrawrect
In my Visual.cpp I also include "ecl.h" as:
#include "ecl.h"
int main() {
ECL_Drawrect (0,0,20,20,false);
return 0;
}
The problem is that the function ECL_bDrawrect is in the ecl.o file and i don't know how to link it to use the function mentioned above.
After some research I figured out and setup as follow:
g++ Visual.cpp -o Visual /usr/include/ecl.o
The ecl.o path also incluced in my project and when I compiled i get this error:
**** Build of configuration Debug for project Visual ****
make all
Building file: ../src/Visual.cpp
Invoking: GCC C++ Compiler
g++ -m32 -O0 -g3 -Wall -c -fmessage-length=0 /usr/include/ecl.o -MMD -MP -MF"src/Visual.d" -MT"src/Visual.d" -o "src/Visual.o" "../src/Visual.cpp"
g++: /usr/include/ecl.o: linker input file unused because linking not done
Finished building: ../src/Visual.cpp
Building target: Visual
Invoking: GCC C++ Linker
g++ -m32 -o "Visual" ./src/Visual.o
/usr/bin/ld: ./src/Visual.o: in function main:../src/Visual.cpp:7: error: undefined reference to 'ECL_bDrawrect'
collect2: ld returned 1 exit status
make: *** [Visual] Error 1
**** Build Finished ****
I don't know whether the link is correct or not? How can I link this ecl.o file in properly way?
p/S: I'm using eclipse CDT in ubuntu 11.04 64-bit and the ecl.o is for 32-bit, that's why I have to put -m32 to g++.

The ecl.o file is not listed on the linker command line.
Also, you probably need to use extern "C" around the include:
extern "C" {
#include "ecl.h"
}

Is it possible that the problem is C++ name mangling? Maybe declaring your functions as extern "C" it will work.
#ifdef __cplusplus
extern "C" {
#endif
/* ... */
#ifdef __cplusplus
}
#endif

Related

Running C++ (with STL) functions in a C application

I am creating a C++ library with exported C functions that use some STL functionality. I want to include the this library in a C application.
I have reduced the problem as much as I could to the following 4 files.
main.c
#include "aaa.h"
#include <stdio.h>
int main()
{
printf("Version: %u.%u\n", GetAPIMajorVersion(), GetAPIMinorVersion());
return 0;
}
aaa.h
#ifndef AAA_H
#define AAA_H
#ifdef __cplusplus
#define DllExport extern "C"
#else // __cplusplus
#define DllExport
#endif // __cplusplus
#include <stdint.h>
DllExport uint32_t GetAPIMajorVersion();
DllExport uint32_t GetAPIMinorVersion();
#endif // AAA_H
aaa.cpp
#include "aaa.h"
#include <string>
#include <vector>
// Builds and works fine.
uint32_t GetAPIMajorVersion()
{
std::string val = "hello world";
return val.size();
}
// Produces the error messages
uint32_t GetAPIMinorVersion()
{
std::vector<bool> test;
test.push_back(true);
return test.size();
}
I am using the following script to build the library and the application.
build.sh
# Build the C++ library
g++ -m64 -Wall -O3 -c -fmessage-length=0 -fPIC -MMD -MP aaa.cpp -o aaa.o
ar rcs libaaa.a aaa.o
# Build the executable
gcc -m64 -Wall -static main.c -o main -L./ -laaa
I get the following errors when I try to build the C application
.//libaaa.a(aaa.o): In function `GetAPIMinorVersion':
aaa.cpp:(.text+0xeb): undefined reference to `operator delete(void*)'
aaa.cpp:(.text+0x1c7): undefined reference to `operator delete(void*)'
.//libaaa.a(aaa.o): In function `std::vector<bool, std::allocator<bool> >::_M_insert_aux(std::_Bit_iterator, bool)':
aaa.cpp:(.text._ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb[_ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb]+0x1d8): undefined reference to `operator new(unsigned long)'aaa.cpp:(.text._ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb[_ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb]+0x339): undefined reference to `operator delete(void*)'
aaa.cpp:(.text._ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb[_ZNSt6vectorIbSaIbEE13_M_insert_auxESt13_Bit_iteratorb]+0x3cb): undefined reference to `std::__throw_length_error(char const*)'.//libaaa.a(aaa.o):(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
I looked into this error and it seems to be because the C application does not have access to the STL libraries but If we alter main.c to only make a call to the GetAPIMajorVersion() and remove the GetAPIMinorVersion() function from the library. The application compiles and runs as expected.
That leads me to believe that the issue is not with the STL library in general but with some of the functions in the STL library.
My next guess is that it is possible that the std::vector<bool>::push_back() function could throw a exception and this is including elements into the aaa.a library that the C application can not find.
If this is the issue then, how do I include the require parts of the STL library in the aaa.a library so it can be used by the C Application?
I have found that if I change the C application to be build with g++ instead of gcc it builds and runs fine. Unfortunately the compiler that I am using in the end only supports C99 and this is not an option for me.
g++ -m64 -Wall -static main.c -o main -L./ -laaa
How should I build this library, that includes STL functions, in a way that the library functions can be called from a C application?
Edit
The compiler that I am using at the end is Arm Keil
There does not seem to be an option to include the stdc++ as a library in the Arm Keil IDE/Compiler. I can't change the command to build the C application to gcc -m64 -Wall -static main.c -o main -L./ -laaa -lstdc++ as far as I am aware.
You could try to build a C++ shared library, linking -lstdc++.
So let -laaa be a shared library libaaa.so (from source files aaa1.cc and aaa2.cc, and having position-independent code) that you would build with:
g++ -fPIC -O3 -g aaa1.cc -o aaa1.pic.o
g++ -fPIC -O3 -g aaa2.cc -o aaa2.pic.o
g++ -fPIC -shared -O3 -g aaa1.pic.o aaa2.pic.o -lstdc++ -o libaaa.so
You might also set some rpath.
Read Program Library HowTo and Drepper's How to write shared libraries
The compiler that I am using at the end is Arm Keil
You'll better use instead some recent version of a GCC cross-compiler (or of Clang one). Either you build that cross-compiler yourself from the source code of GCC 8 (in autumn 2018), or you install some cross-compiler on your Linux distribution. For example, Debian/Sid has gcc-8-arm-linux-gnueabi and gcc-8-arm-linux-gnueabihf
By experience, hardware vendors provide ancient cross-compilers (and are not good in software engineering). That is why I recommend using a recent GCC cross-compiler, on the command line.
And you'll better link your application with g++.
My next guess is that it is possible that the std::vector::push_back() function could throw a exception
Exceptions need some support at the crt0 level (for std::terminate). If your library throws some exception, the main program has to be linked with g++ (if you want a C++ library usable from C, it should not throw exception outside).
However, it is possible, with some care, to build a C++ library usable from gcc-compiled C code. The libgccjit is such a library (but it does not throw exceptions outside).
I can't change the command to build the C application to gcc -m64 -Wall -static main.c -o main -L./ -laaa -lstdc++ as far as I am aware
You surely could. You need to avoid using Arm Kell and use directly the appropriate cross-compiler on the command line (either the one supplied inside it, or preferably a more recent one that you build from GCC source code or Clang one).

How to include a library header after adding the linker in Eclipse

I am trying to include a library from Github (https://github.com/dcjones/hat-trie).
The installation worked fine and I was able to add the library
to the linker in Eclipse
but the problem is that I don't know how to use it.
I have added it as a header
#include <hat-trie.h>
but it's giving this error:
18:49:31 **** Incremental Build of configuration Debug for project test2 ****
make all
Building file: ../src/test2.cpp
Invoking: GCC C++ Compiler
g++ -std=c++1y -std=c++11 -I/home/use1/boost_1_58_0 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/test2.d" -MT"src/test2.d" -o "src/test2.o" "../src/test2.cpp"
**../src/test2.cpp:28:22: fatal error: hat-trie.h: No such file or directory
#include <hat-trie.h>**
^
compilation terminated.
make: *** [src/test2.o] Error 1
18:49:33 Build Finished (took 2s.831ms)
Any idea how can I fix this?

Linking my program using g++

I have some problems to build a program using g++. The program is using a library that I have written in C called libiec60063. I want to write my new project in C++ (even if not yet familiar with C++) but I can't manage to link it correctly.
For example I have the following code in a file called main.cpp
#include <libiec60063.h>
int main() {
Select_IEC60063_Number(125, 12);
return 0;
}
I can compile the source correctly typing
g++ -I/home/workspace/a_CommonBinary/include -c main.cpp
If I want to link it i get some error message
g++ -L/home/workspace/a_CommonBinary_draft/lib -o main main.o -lm -liec60063
main.o: In function `main':
main.cpp:(.text+0x1b): undefined reference to `Select_IEC60063_Number(double, int)'
collect2: error: ld returned 1 exit status
If I rename the main-file to main.c I can compile and link the program correctly with the GCC-Compiler using the same parameters.
Can anybody explain where there is a difference between gcc and g++?
You probably forgot to put
#ifdef __cplusplus
extern "C" {
#endif
near the beginning of your libiec60063.h header file, and
#ifdef __cplusplus
}; // end extern "C"
#endif
near the end of your header file, or if you don't want to change the header file:
extern "C" {
#include <libiec60063.h>
};
in your C++ code.
See this question. You need to disable name mangling. Read about compatibility of C & C++ .
BTW, you should compile with g++ -Wall -Wextra -g and perhaps with -std=c++11 and code for C++11

Linker error in switching from cc compiler to g++ compiler

I have been using C to code for sometime and I had no issues in compiling and linking my code to archive libraries (.a files). However, now I need to switch to C++ and doing so, I have to use g++ instead of cc for linking and compiling.
Using C, first thing to do was compiling the source and creating the object file and then linking it to the library using the same command but without the -c option:
cc -c -ggdb -Wall -Werror -I.. test.c -o test.o
cc -o test -ggdb -Wall -Werror test.o ../libpmem/libpmem.a
As I think the same procedure should be done for doing the same thing with g++, I tried to change the compiling and linking phase as follow:
g++ -c -ggdb -Wall -Werror -I.. test.c -o test.o
g++ test.o -I.. -L/path/libpmem -lpmem -o test
Although both sets should do the same thing, I always get an error while trying to link using g++. Here is the error message:
test.o: In function `main':
/path/test/test.c:5: undefined reference to `pmem_msync_mode()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
There must be something wrong with the linking phase as the method definition must be found in the library file (just as the cc linker can find the definition and do the linking without any problem).
I also tried to do both linking and compiling using a single g++ command, but no matter what I do, I always get the same error. Any idea how can I fix this?
you probably have some sort of name mangling problem...
since it looks like that is declared in a c library there should already be some sort of
#ifdef __cplusplus
extern "C" {
#endif
int pmem_msync_mode();
#ifdef __cplusplus
}
#endif
but if there isn't in the library header you may have to do something like;
extern "C" {
#include <pmem.h>
}
See http://www.cplusplus.com/forum/general/1143/
You need to tell the compiler that the code is compiled as C
I am assuming that the pmem library is compiled and linked as C code.
C and C++ have different conventions for how they store the names of functions in binary format. See http://en.wikipedia.org/wiki/Name_mangling for a reference.
To solve your problem, probably the solution is to:
extern "C" {
#include <pmem.h>
}
When the C++ compiler imports the function declarations from the header, without external C linkage being specified, it expects the binary it links against to have the associated functions' representations in a different format, namely whatever format your C++ compiler mangles function names to.

ld reports undefined reference to symbol found by nm

I am building a shared library with the following instructions :
cc -shared -Wl,-soname,libmy.so.0 [lots of .o files] -o libmy.so.0
ln -f -s libmy.so.0 libmy.so
I don't know if this is important, but this library is written in C.
I then try to link with this library, from a C++ program :
/usr/bin/c++ -g CMakeFiles/codegen.dir/client/codegen.cpp.o CMakeFiles/codegen.dir
/namenode/shared_objects.cpp.o -o codegen -rdynamic -L/path/to/libmy -lpthread
-lboost_system-mt -lboost_filesystem-mt -lboost_unit_test_framework-mt
-lboost_serialization-mt -lmy -Wl,-rpath,/path/to/libmy
But ld reports an error :
CMakeFiles/codegen.dir/client/codegen.cpp.o: In function `main':
[...]/src/client/codegen.cpp:46:
undefined reference to `alloc_code(int, int, int, int, int)'
Even if alloc_code appears to be in the shared library:
$ nm libmy.so | grep alloc_code
0000000000002c80 T alloc_code
Note that libmy.so and my codegen program are compiled using different compiler flags (one is compiled in debug mode while the other is compiled in optimized mode), but I don't think the issue comes from here.
What could make ld unable to link codegen with libmy.so ?
Your C function needs to be declared to C++ as this:
#ifdef __cplusplus
extern "C" {
#endif
void alloc_code(int, int, int, int, int);
#ifdef __cplusplus
}
#endif
Otherwise, the compiler and thus linker assume C++ linkage (where multiple functions can exist with the same name so long as their arguments differ) - and will search for the "C++-mangled" variant of the function.
When using extern "C" you tell the compiler and linker to use the unmangled "C" name.
To demonstrate this, let's write the same "library" and compile it with both a C compiler and a C++ compiler:
/* Library Source Code */
int library_function(void) {
return 0;
}
Then compile with C:
gcc -o libdummy.so -shared -fPIC dummy-library.c
nm libdummy.so | grep library_function
Which outputs:
0000000000000640 T library_function
Running the same thing with a C++ compiler yields:
g++ -o libdummy.so -shared -fPIC dummy-library.c
nm libdummy.so
=>
0000000000000670 T _Z16library_functionv