Linking shared libraries with executables - c++

I have a small doubt in the compilation of a c++ code along with a shared library.
So I have two files main.cpp and sample.cpp.
main.cpp
#include <iostream>
using namespace std;
#include "sample.h"
myStruct obj;
void populateData() {
obj.s = "hello world";
}
myStruct giveData() {
cout << "Inside main: " << obj.s << endl;
return obj;
}
int main() {
populateData();
}
sample.h
#ifndef SAMPLE_H
#define SAMPLE_H
#include <string>
struct myStruct {
std::string s;
void populateData();
};
myStruct giveData();
#endif
sample.cpp
#include "sample.h"
#include <iostream>
#include <boost/python.hpp>
using namespace std;
void myStruct :: populateData() {
cout << giveData().s;
}
BOOST_PYTHON_MODULE(boosts) {
using namespace boost::python;
class_<myStruct>("struct")
.add_property("s", &myStruct::s)
.def("populateData", &myStruct::populateData)
;
}
I compile the program using
g++ -c -fPIC sample.cpp
g++ -c -fPIC main.cpp
g++ -shared -Wl,-soname,boosts.so -o boosts.so sample.o main.o -lpython2.7 -lboost_python
g++ -o main main.o
./main
Now, when I run the main, it populates the string inside the obj. But when I run a python script, that imports the boosts.so, the obj.s is empty.
I am guessing it is because the library boosts.so is not properly linked with the executable main.
How do I fix this?

Related

How can i use Rcpp as a 3rd library for C++?

I can't use Rcpp as a 3rd party library for C++.
My sample code is as follows:
main.cpp
#include <Rcpp.h>
#include <iostream>
using namespace Rcpp;
int main() {
std::cout << 1;
Rcpp::ListMatrix a;
system("pause");
return 0;
}
Then I compile using g++
g++ main.cpp -E -o main.i -I "D:/R-4.1.0/include" -I "D:/R-4.1.0/library/Rcpp/include"
g++ -S main.i -o main.s
g++ -c main.s -o main.o
g++ main.o -L"D:\R-4.1.0\library\Rcpp\libs\x64" -l "Rcpp" -L"D:\R-4.1.0\bin\x64" -l "R" -o main.exe
It successfully spawned main.exe.
But when I run main.exe, the program throws an exception.
It shows that these codes in Rcpp cannot run.
Vector( const Dimension& dims) {
Storage::set__( Rf_allocVector( RTYPE, dims.prod() ) ) ;
init() ;
if( dims.size() > 1 ){
AttributeProxyPolicy<Vector>::attr( "dim" ) = dims;
}
}
Then I found that when the constructors of vector in Rcpp all have functions prefixed with Rf, exceptions are thrown.
why this happen?
I try this,and it it didn't throw exception.
#include <Rcpp.h>
#include <iostream>
using namespace Rcpp;
int main() {
std::cout << 1;
Rcpp::S4 a;
system("pause");
return 0;
}
I think it may have something to do with the function prefixed with Rf.

C++ clang compiling & linking on ipad, Linker issue

please may you advise how I may compile & run main.cpp while compiling and linking the my_class.cpp & my_class.h class files,please note that this is running on an iPad using the “Code” app by “thebaselab”, which has offline clang++ 13.0. developer says its possible to work using the below method, however theres no output.
I compile seperately:
clang++ main.cpp -c
clang++ my_class.cpp - c
It seems to produce the main.o and my_class.o files so I may run using:
clang++ main.o my_class.o
This doesnt seem to run as no output, please may you advise if you can see the problem in my code or in compiling.?
I believe there is an issue with linking these files together, as when I have the class defined in main there’s no issues.
My code base:
main.cpp
// Created on iPad.
#include <iostream>
#include <vector>
#include "my_class.h"
using namespace std;
int main() {
cout << "Hey\n";
my_class obj1 = my_class("test");
obj1.display();
cout << "Hello World!";
return 0;
}
my_class.h
#ifndef _my_class_H_
#define _my_class_H_
class my_class
{
private:
std::string name = "";
public:
my_class(std::string name_tmp); // No-args constructor // Copy constructor
~my_class(); // Destructor
void display();
};
#endif
my_class.cpp
#include <iostream>
#include "my_class.h"
// 1-args constructor
my_class::my_class(std::string name_tmp){
name = name_tmp;
}
// Destructor
my_class::~my_class() {
std::cout << "Destructing\n";
}
void my_class::display(){
std::cout << name << "\n";
}
Thanks to #Quimby,
I ran clang++ -o a.out main.o my_class.o then just a.out and it works on my iPad.

Undefined reference in a simple makefile test CPP

I was trying to make a simple makefile to learn how to use headers and make in C++, however it doesn't seems to work, returning "undefined reference" at the end. What is going wrong here? I don't believe there are any mistakes in the .cpp or .hpp files, and I searched a bit about makefile to be sure that it should be working.
My main.cpp:
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
int main(int argc, char *argv[]){
test::print();
return 0;
}
For test.hpp and test.cpp:
#include <iostream>
#include <fstream>
#pragma once
class test{
private:
public:
static void print();
};
#include <iostream>
#include <fstream>
#include "test.hpp"
using namespace std;
static void print(){
cout << "aaaaaa\n";
}
Finally, the makefile:
all:
rm *.o
g++ -c -g *.cpp
g++ *.o -o main.exe
rm *.o

ldfcn.h gives 0 instead of true or false

i have dummy.cpp
#include <iostream>
#ifndef EXPORT_API
#define EXPORT_API __attribute__ ((visibility("default")))
#endif
extern "C"{
using namespace std;
bool dum = true;
int main(){};
};
and main.cpp
#include <iostream>
#include <dlfcn.h>
#include <string>
using namespace std;
int main()
{
void *test = dlopen("./dummy", RTLD_NOW);
//bool sus = reinterpret_cast<void(*)()>(dlsym(test , "turd"));
bool* give =(bool*) dlsym(test, "dum");
cout<<give<<"refrence";
};
and i compile them with
g++ dummy.cpp -o dummy
g++ main.cpp -o main -ldl
but when i run ./exe
i get 0refrence and i dont know how to fix this or what the issue is i also have tried making it a function and returning it but that didn't work
solved it with gcc -shared -o dummy.so -fPIC dummy.cpp to get a proper so file
and if yours has a function like int foo(){} or something
use std::invoke in your .sofile and then call it like
bool* give = (bool *) dlsym(test, "dum");
auto answr = *give;
where answr is the return val

Moving helper function to header file

In the beginning I had:
main.cpp
#include "something.h"
#include "util.h"
int main() {
sth::something();
utl::little_thing();
}
somehing.h
#ifndef SOMETHING_H
#define SOMETHING_H
namespace sth {
void something();
}
#endif
somehing.cpp
#include "something.h"
#include <string>
#include <iostream>
namespace sth {
void print_me(std::string txt) {
std::cout << txt << std::endl;
}
void something() {
std::cout << "this is something" << std::endl;
print_me("optional");
}
}
util.h
#ifndef UTIL_H
#define UTIL_H
namespace utl {
void little_thing();
}
#endif
util.cpp
#include "util.h"
#include <iostream>
#include <string>
namespace utl {
void little_thing() {
std::cout << "this is little thing" << std::endl;
}
}
Then I though it will be better to have print_me(std::string txt) out of sth namespace. And I put it on utl, declaration on .h file and definition on .cpp file.
At that point the lazy side of me said - would it be better to have it all in one file as was beofore. And I tried:
util.h
#ifndef UTIL_H
#define UTIL_H
#include <string>
#include <iostream>
namespace utl {
void little_thing();
void print_me(std::string txt) {
std::cout << txt << std::endl;
}
}
#endif
something.cpp
#include "something.h"
#include "util.h"
#include <string>
#include <iostream>
namespace sth {
void something() {
std::cout << "this is something" << std::endl;
utl::print_me("optional");
}
}
So I got:
c++ -std=gnu++14 -g -Wall -O3 -c -o main.o main.cpp
c++ -std=gnu++14 -g -Wall -O3 -c -o util.o util.cpp
c++ -std=gnu++14 -g -Wall -O3 -c -o something.o something.cpp
c++ main.o util.o something.o -o main
duplicate symbol __ZN3utl8print_meENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE in:
main.o
util.o
duplicate symbol __ZN3utl8print_meENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE in:
main.o
something.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
Which makes sense to me, since util.h is included in main.cpp and something.cpp there are duplicate symbols, right?
Question, is it possible to be as lazy to have it all in the header? Or no way, have to split decalration and definition? I don't care (in this case) to hide the implementation in the .cpp, I just want to move it out of sth.
Maybe move the definition of print_me down to util.cpp and only leave a declaration of it in the util.h file.
Else you get a copy of it in every object file, and then the linker gets confused as they all have the same name (symbol).