ldfcn.h gives 0 instead of true or false - c++

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

Related

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

gcc --version-script cannot recognize int64_t

The code examples explain the problem fairly straightforward:
Hash.h
#include <iostream>
#include <cstdint>
using namespace std;
class Hash {
public:
Hash(int64_t sz);
int64_t size;
};
Hash.cpp
#include "Hash.h"
using namespace std;
Hash::Hash(int64_t sz) : size(sz)
{
cout << "Hash int" << endl;
}
main.cpp
#include "Hash.h"
using namespace std;
int main(int argc, char *argv[])
{
Hash HashTable(12);
return 0;
}
And here is the version file, foo.map:
VER_0.1
{
global:
extern "C++" {
"Hash::Hash(int64_t)";
};
local:
*;
};
For the compilation:
$g++ -g -c -Wall -Werror -fpic Hash.cpp -std=c++0x
$g++ -shared -o Hash.so Hash.o -std=c++0x -Wl,--version-script=foo.map
$g++ -g -o prog Hash.so main.cpp -std=c++0x
The error message:
/tmp/ccd60Ulm.o: In function `main':
/remote/ltg_engine1_us03/liangwa/test/004/main.cpp:7: undefined reference to `Hash::Hash(long)'
collect2: error: ld returned 1 exit status
Then if I change all int64_t to int or long, it compiles fine. So anything special with int64_t? I am using g++ 7.3.0

Linking shared libraries with executables

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?

OpenCV errors when including it's headers in C++

I'm trying to use OpenCV into an existing project; when I add the header #include <opencv2/opencv.hpp> to my source.cc file (just the header, the rest of the code makes no difference), I get the following error.
make all
Building file: source.cc
Invoking: GCC C++ Compiler
g++ -I../__GXX_EXPERIMENTAL_CXX0X__ -I/usr/include/eigen3 -O3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"source.d" -MT"source.d" -o "source.o" "source.cc"
In file included from /usr/local/include/opencv2/core.hpp:3105:0,
from /usr/local/include/opencv2/opencv.hpp:46,
from source.cc:13:
/usr/local/include/opencv2/core/operations.hpp: In member function ‘bool cv::internal::Matx_FastSolveOp<_Tp, m, n>::operator()(const cv::Matx<_Tp, m, m>&, const cv::Matx<_Tp, m, n>&, cv::Matx<_Tp, m, n>&, int) const’:
/usr/local/include/opencv2/core/operations.hpp:131:18: error: missing template arguments before ‘(’ token
return LU(temp.val, m*sizeof(_Tp), m, x.val, n*sizeof(_Tp), n) != 0;
^
make: *** [MapMaker_target_init.o] Error 1
The code looks something like this:
MMaker.cc >>>
#include "MMaker.h"
#include "IMatcher.h"
#include <cvd/image_ref.h>
#include <cvd/vector_image_ref.h>
#include <cvd/vision.h>
#include <cvd/image_interpolate.h>
#include <opencv2/opencv.hpp> // <<<<<<<<<< This is the evil line
bool MMaker::init(AR::IMatcher &matcher)
{
//actually empty
}
MMaker.h >>>
#ifndef __MMAKER_H
#define __MMAKER_H
#include <queue>
namespace AR { class IMatcher; }
class MMaker: protected CVD::Thread
{
public:
bool init(AR::IMatcher &matcher);
// ... more stuff ...
}
//...
IMatcher.h >>>
#ifndef __IMATCHER_H
#define __IMATCHER_H
#include <vector>
#include <string>
namespace cv { class Mat; }
namespace AR
{
class IMatcher
{
// ... IMatcher members declaration ...
}
}
IMatcher.cc >>>
#include "IMatcher.h"
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
// ... IMatcher members implementation ...
I know this is related to how/which files are included or their order perhaps, but haven't got to find what's missing.

Undefined reference to a user defined function

I have seen the standard Undefined Reference to thread from this site but I do not believe it solves my problem. I am not putting header guards on my .cpp files, but still get an undefined reference to a user defined function. Here are my files:
(1) pth_funs.h
// hello from thread <pid>
void* hello(void* ptr);
(2) pth_funs.cpp
#include <stdio.h>
void* hello(void *ptr)
{
char *message;
int pid = (long) ptr;
printf("Hello from thread %i\n", pid);
}
(3) structs.h
#ifndef STRUCTS_H
#define STRUCTS_H
struct grd_str {
long nx;
long ny;
long natoms;
char** atnames;
double* xs;
double* ys;
double** fs;
double** xyzs;
};
#endif
(4) fio.h
#ifndef FIO_H
#define FIO_H
#include <iostream>
#include <string.h>
#include "structs.h"
void read_grd(std::string, grd_str);
#endif
(5) fio.cpp
#include <string.h>
#include "structs.h"
#include "fio.h"
void read_grd( std::string fname, grd_str &grd)
{
grd.nx = 10;
grd.ny = 10;
}
(6) and finally, xdriver.cpp
#include <iostream> // needed for cout, endl, etc
using namespace std; // needed for cout, endl, etc
#include <pthread.h> // needed for pthreads
#include <string.h> // string handling
#include "pth_funs.h" // pthread function headers
#include "structs.h"
#include "fio.h"
int main(int argc, char** argv)
{
// thread stuff
int nthreads = 4;
pthread_t rank[4];
int iret[4];
// file stuff
string base_dir = "D:\\cygwin64\\home\\Robert\\code\\C\\form_reconstruction\\data\\";
string fname;
// topology stuff
int nx, ny;
double* xs;
double* ys;
double** fs;
grd_str grd;
for(long tid = 0; tid < nthreads; tid++)
{ iret[tid] = pthread_create( &rank[tid], NULL, hello, (void*) tid); }
fname = base_dir;
fname.append("adf\\adf.6.grd");
cout << "Filename: " << fname << endl;
read_grd(fname, grd);
}
I am compiling this using a Makefile which is as follows:
cc=g++
exe=create_grd.exe
flags=-pthread
hds= pth_funs.h fio.h structs.h
objs= pth_funs.o fio.o
all: create_grd.exe
create_grd.exe: xdriver.cpp $(hds) $(objs)
$(cc) -o $(exe) $(objs) xdriver.cpp
pth_funs.o: pth_funs.cpp pth_funs.h
$(cc) -c pth_funs.cpp $(flags)
fio.o: fio.cpp fio.h
$(cc) -c fio.cpp $(flags)
clean:
rm -rf *.o
However, upon compilation I get
g++ -c pth_funs.cpp -lpthread
g++ -c fio.cpp -lpthread
g++ -o create_grd.exe pth_funs.o fio.o xdriver.cpp -lpthread
/tmp/ccdaBayB.o: In function `main':
xdriver.cpp:(.text+0x16f): undefined reference to `read_grd(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, grd_str)'
collect2: ld returned 1 exit status
make: *** [create_grd.exe] Error 1
but I have no idea why my main routine can't find read_grd since I believe I am properly defining it and including it. What am I doing wrong?
Your declaration and definition of read_grd do not have matching arguments. One takes a grd_str as its second argument, the other takes a grd_str&. Since xdriver.cpp includes fio.h, it sees and attempts to use the former function, but the linker can't find a definition for it anywhere. Chances are you want to change your declaration in fio.h to:
void read_grd(std::string, grd_str&);
Now the definition for this function is provided by fio.cpp.