building a .so that is also an executable with C++ - c++

Based on building a .so that is also an executable I'm trying o reproduce with C++ and I'm getting a segmentation fault on main program execution.
/* pie.cpp */
#include <cstdio>
int foo()
{
printf("in %s %s:%d\n", __func__, __FILE__, __LINE__);
return 42;
}
int main()
{
printf("in %s %s:%d\n", __func__, __FILE__, __LINE__);
return foo();
}
/* pie.h */
#ifndef PIE_H
#define PIE_H
int foo();
#endif
/* main.cpp */
#include <cstdio>
#include <string>
#include "pie.h"
std::string data;
int main()
{
data="TEST";
printf("in %s %s:%d [%s]\n", __func__, __FILE__, __LINE__, data.c_str());
return foo();
}
$ g++ -fPIC -pie -o pie.so pie.cpp -Wl,-E
$ g++ main.cpp ./pie.so
$ ./pie.so
in main pie.cpp:10
in foo pie.cpp:5
$ ./a.out
Segmentation fault (core dumped)
$
I the definition of "data" is moved from global to local it runs.
It seems that global variables are not being initialized.
Can someone explain what happens and what should be done to make it run?
gdb backtrace on code file result:
Program terminated with signal SIGSEGV, Segmentation fault.
#0 std::string::size (this=0x404080 <data>) at /usr/src/debug/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:716
716 { return _M_rep()->_M_length; }
(gdb) bt
#0 std::string::size (this=0x404080 <data>) at /usr/src/debug/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:716
#1 std::string::assign (this=0x404080 <data>, __s=0x402010 "TEST", __n=4) at /usr/src/debug/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:262
#2 0x00000000004011c5 in main () at main.cpp:9
Thanks!

I just tried code from my old answer; it no longer works using a recent GLIBC (I have 2.31-9+build1):
$ gcc -fPIC -pie -o pie.so pie.c -Wl,-E
$ gcc main.c ./pie.so
$ ./pie.so
in main pie.c:10
in foo pie.c:5
$ ./a.out
./a.out: error while loading shared libraries: ./pie.so: cannot dynamically load position-independent executable
Using your C++ example, I can't build it with your commands:
$ g++ -fPIC -pie -o pie.so pie.cc -Wl,-E
$ gcc main.cc ./pie.so
/usr/bin/ld: /tmp/ccaXra73.o: in function `main':
main.cc:(.text+0x13): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)'
/usr/bin/ld: main.cc:(.text+0x1f): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
/usr/bin/ld: /tmp/ccaXra73.o: in function `__static_initialization_and_destruction_0(int, int)':
main.cc:(.text+0x77): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
/usr/bin/ld: main.cc:(.text+0x8c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
collect2: error: ld returned 1 exit status
If I link with g++ (as you should when you have C++ code), then it links fine but fails the same way as C code does:
$ g++ main.cc ./pie.so
$ ./pie.so
in main pie.cc:9
in foo pie.cc:4
$ ./a.out
./a.out: error while loading shared libraries: ./pie.so: cannot dynamically load position-independent executable
So I guess the answer is: it wasn't intended to work this way (and worked in the past "by accident"), and now GLIBC detects and rejects it.

Related

undefined reference to dlopen

I have one C program which I want to load into my running C program. Following are the snippet
File : a.c
#include <stdio.h>
void abc() {
printf("This is abc\n");
}
File : mainFile.cpp
#include<stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *lib = dlopen("./a.so", RTLD_LAZY);
if (!lib) {
printf("dlopen failed: %s\n", dlerror());
return 1;
}
void (*f)() = dlsym(lib, "abc");
if (f) {
f();
} else {
printf("dlsym for f1 failed: %s\n", dlerror());
}
dlclose(lib);
return 0;
}
I am compiling with the following commands
gcc -fpic -g -shared -ldl -o a.so a.c
g++ -w mainFile.cpp -o mainFile
Output:
/tmp/cc9fYZaf.o: In function `main':
mainFile.cpp:(.text+0x1a): undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
I am compiling in Ubuntu 16.04 with gcc version gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609
Please help
Note: I have followed the following references but none helped.
Can you dynamically compile and link/load C code into a C program?
undefined reference to `dlopen' since ubuntu upgrade
undefined reference to `dlopen'
The second line — the one which links the executable — needs the -ldl, not the first:
g++ -w mainFile.cpp -ldl -o mainFile

Cannot Call C++ Code from C without Error [duplicate]

This question already has answers here:
How to mix C++ and C correctly
(2 answers)
Closed 4 years ago.
I'm trying to write a C++ library that can be called from C. However, whenever I try to even write a bare minimum example, it crashes with undefined references. Here is my code:
mylibrary.h
#ifndef __MY_CPP_THING_H
#define __MY_CPP_THING_H
#ifdef __cplusplus
extern "C" {
#endif
void printSomething();
#ifdef __cplusplus
}
#endif
#endif
mylibrary.cpp
#include <iostream>
#include "mylibrary.h"
extern "C" {
void printSomething() {
std::cout << "PLEASE PRINT\n";
}
}
main.c
#include "mylibrary.h"
int main() {
printSomething();
return 0;
}
The compiling process goes something like this:
g++ -c mylibrary.cpp -o mylibrary.o (create "mylibrary.o")
ar rcs libmylibrary.a mylibrary.o (create static library "libmylibrary.a")
gcc main.c -L. -lmylibrary (link static library and compile C source file)
However, I receive this error dump:
mylibrary.o:mylibrary.cpp:(.text+0x17): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
mylibrary.o:mylibrary.cpp:(.text+0x32): undefined reference to `std::ios_base::Init::~Init()'
mylibrary.o:mylibrary.cpp:(.text+0x62): undefined reference to `std::ios_base::Init::Init()'
mylibrary.o:mylibrary.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout'
collect2.exe: error: ld returned 1 exit status
Any suggestions on how to resolve the error?
mylibrary.o still depends on C++ standard library and gcc doesn't know about it. Call gcc with -lstdc++ in the last step.
Creating a dynamic library instead of a static library should do the trick :
$ gcc -c main.c
$ g++ -fPIC -shared -o mylibrary.so mylibrary.cpp
$ gcc -o main main.o mylibrary.so
and then :
$ LD_LIBRARY_PATH=".:${LD_LIBRARY_PATH}" ./main
PLEASE PRINT
with :
$ objdump -p main | grep NEEDED
NEEDED mylibrary.so
NEEDED libc.so.6
$ objdump -p mylibrary.so | grep NEEDED
NEEDED libstdc++.so.6
NEEDED libc.so.6

Can't compile code with CVC4 C++ API

I am just trying to compile this file helloworld.cpp
#include <iostream>
#include <cvc4/cvc4.h>
using namespace CVC4;
int main() {
ExprManager em;
Expr helloworld = em.mkVar("Hello World!", em.booleanType());
SmtEngine smt(&em);
std::cout << helloworld << " is " << smt.query(helloworld) << std::endl;
return 0;
}
using g++ helloworld.cpp -lcvc4 -o helloworld -lcvc4 -Wno-deprecated. But it is giving me this error
/tmp/cc9SFpL4.o: In function `main':
helloworld.cpp:(.text+0xac): undefined reference to `CVC4::ExprManager::mkVar(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, CVC4::Type, unsigned int)'
collect2: error: ld returned 1 exit status
Help!
I have installed CVC4 adding repo link in /etc/apt/sources.list and then calling sudo apt-get install cvc4 libcvc4-dev libcvc4parser-dev.
EDIT: I mistyped g++ helloworld.cpp -lcvc4 ... I used g++ helloworld.cpp -o helloworld -lcvc4 -Wno-deprecated. Actually I used all combinations, permutations.
This seems to be a problem with the OP's environment. Both r4C9rAyrd6A1 and I were able to compile the example on our local machines. The specific issue might have been that the OP's compiler wanted the -lcvc4 linker flag after the other flags, e.g. g++ helloworld.cpp -Wno-deprecated -o helloworld -lcvc4 as mentioned in the comments.

Problems with wrapping a C++ code

To try the C++ code wrapping within C, I used the following:
header.h
#ifdef __cplusplus
extern "C"
#endif
void func();
source.cpp
#include "header.h"
#include <iostream>
extern "C" void func()
{
std::cout << "This is C++ code!" << std::endl;
}
and source.c
#include "header.h"
int main()
{
func();
}
To compile and link, I used the following sequence:
g++ -c source.cpp
gcc source.c source.o -o myprog
The error I get is:
ence to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
source.cpp:(.text+0x1c): undefined reference tostd::basic_ostream >::operator<<(std::basic_ostream >& (*)(std::basic_ostream >&))'
source.o: In function __static_initialization_and_destruction_0(int, int)':
source.cpp:(.text+0x45): undefined reference tostd::ios_base::Init::Init()'
source.cpp:(.text+0x4a): undefined reference to std::ios_base::Init::~Init()'
source.o:(.eh_frame+0x12): undefined reference to__gxx_personality_v0'
collect2: ld returned 1 exit status
How can I make this simple code compile and run? It should serve as a basis for my future
development.
Link with g++ as well:
g++ -c source.cpp
g++ source.c source.o -o myprog
Or better:
g++ -c source.cpp -o source_cpp.o
gcc -c source.c -o source_c.o
g++ -o myprog source_cpp.o source_c.o
Best to avoid the common prefix source.{cpp,c} as it causes confusion.
You'll still have to link with the C++ linker:
gcc -o source-c.o source.c
g++ -o source-p.o source.cpp
g++ -o myprog source-c.o source-p.o
Your C++ object file will need to resolve symbols from the C++ library, which only the C++ linker will pull in automatically. (Alternatively, you could specify the library manually for the C linker.)
you can't do that, you can compile C code with gcc, and then link it with c++ objects with g++, referring to the symbols defined in the C objects via the extern keyword, but you can't do the opposite

CppUnit, very simple test code crashed, why?

I am learning CppUnit, and my code coredumped when calling TestRunner::run(). That's line 34 in the below code. I can not see anything wrong. It is almost identical to the sample code in the CppUnit cookbook.
#include <iostream>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
#include <cppunit/ui/text/TestRunner.h>
using namespace std;
//class MyFixture: public CppUnit::TestFixture {
class MyFixture{
public:
MyFixture() {cout<<"MyFixture:: ctor()"<<endl;}
~MyFixture() {cout<<"MyFixture:: dtor()"<<endl;}
void setUp() {cout<<"MyFixture::Setup()"<<endl;}
void tearDown() {cout<<"MyFixture::tearDown()"<<endl;}
void testFunc1() {cout<<"MyFixture::testFunc1()"<<endl; m=1; CPPUNIT_ASSERT(m==1);}
void testFunc2() {cout<<"MyFixture::testFunc2()"<<endl; m=2;}
int m;
static CppUnit::TestSuite * CreateSuite();
};
CppUnit::TestSuite * MyFixture::CreateSuite()
{
CppUnit::TestSuite * suite = new CppUnit::TestSuite("My TestSuite for MyFixture");
suite->addTest(new CppUnit::TestCaller<MyFixture>("MyFixture::testFunc1", &MyFixture::testFunc1));
suite->addTest(new CppUnit::TestCaller<MyFixture>("MyFixture::testFunc2", &MyFixture::testFunc2));
}
int main()
{
cout<<"point 1000"<<endl;
CppUnit::TextUi::TestRunner runner;
runner.addTest(MyFixture::CreateSuite());
cout<<"point 7000"<<endl;
/*Line34*/ runner.run();
cout<<"point 8000"<<endl;
}
The output:
cppunit$ ./test_runner
point 1000
MyFixture:: ctor()
MyFixture:: ctor()
point 7000
Segmentation fault (core dumped)
The stack trace:
(gdb)bt
-0 0x00733cc9 in CppUnit::TestRunner::WrappingSuite::run(CppUnit::TestResult*) ()
from /usr/lib/libcppunit-1.12.so.1
-1 0x0073170a in CppUnit::TestResult::runTest(CppUnit::Test*) () from /usr/lib/libcppunit-1.12.so.1
-2 0x00733af0 in CppUnit::TestRunner::run(CppUnit::TestResult&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/libcppunit-1.12.so.1
-3 0x00736d2b in CppUnit::TextTestRunner::run(CppUnit::TestResult&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/libcppunit-1.12.so.1
-4 0x00736da2 in CppUnit::TextTestRunner::run(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, bool, bool) () from /usr/lib/libcppunit-1.12.so.1
-5 0x080494dd in main () at test_runner.cpp:34
The compile line: g++ -g -o -Wall test_runner test_runner.cpp -lcppunit
It compile successfully without any warning.
If I turned on "-Wall", it gave error:
......
(.data+0x4): multiple definition of `__dso_handle'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/crtbegin.o:(.data+0x0): first defined here
test_runner: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crti.o:(.init+0x0): first defined here
/tmp/ccgzqvu9.o: In function `MyFixture::CreateSuite()':
/home/fzhao/temp/cppunit/test_runner.cpp:22: multiple definition of `MyFixture::CreateSuite()'
test_runner:/home/fzhao/temp/cppunit/test_runner.cpp:22: first defined here
/tmp/ccgzqvu9.o: In function `main':
/home/fzhao/temp/cppunit/test_runner.cpp:29: multiple definition of `main'
test_runner:/home/fzhao/temp/cppunit/test_runner.cpp:29: first defined here
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__'
test_runner:(.dtors+0x4): first defined here
/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored.
/usr/bin/ld: error in test_runner(.eh_frame); no .eh_frame_hdr table will be created.
collect2: ld returned 1 exit status
make: *** [test_runner] Error 1
Given that the signature for the function is:
CppUnit::TestSuite * MyFixture::CreateSuite()
shouldn't you be returning something (suite would be my first guess)?
By not returning anything, the line:
runner.addTest(MyFixture::CreateSuite());
is going to add a very dodgy pointer to your runner.
And the reason you're probably getting those errors when you insert -Wall is because:
g++ -g -o -Wall test_runner test_runner.cpp -lcppunit
\______/ \___________________________________/
will try to output your executable to the file -Wall by linking together test_runner, test_runner.cpp and the cppunit library. Perhaps you meant to type:
g++ -g -Wall -o test_runner test_runner.cpp -lcppunit
\____________/ \_______________________/
(I'm assuming here that the compile line you gave was the one where the errors occurred since it actually has -Wall in it ).