共享库如何链接到符号?
我有: car.cc
#include "car.h"
#include <iostream>
using namespace std;
extern "C" Car* create_object()
{
return new Car;
}
Car::Car() {
this->maxGear = 2;
this->currentGear = 1;
this->speed = 0;
}
void Car::shift(int gear) {
if (gear < 1 || gear > maxGear) {
return;
}
currentGear = gear;
}
void Car::brake() {
speed -= (5 * this->getCurrentGear());
std::cout<<"THE SPEED IS:" <<speed<<std::endl;
}
extern "C" void destroy_object( Car* object )
{
delete object;
}
car.h
#ifndef VEHICLES_CAR_H
#define VEHICLES_CAR_H
// A very simple car class
class Car {
public:
Car();
void shift(int gear);
void accelerate();
void brake();
private:
int maxGear;
int currentGear;
int speed;
};
#endif /* VEHICLES_CAR_H */
test.cc
#include "/home/car.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
/* on Linux, use "./myclass.so" */
void* handle = dlopen("/usr/lib/libCarTest.so", RTLD_LAZY);
int (*result)(int);
if (!handle)
{
}
/*dlsym(handle,"accelerate");
cout<<"IN HERE: "<<endl;
dlsym(handle,"brake");
dlclose(handle);*/
Car* (*create)();
void (*destroy)(Car*);
dlerror();
create = (Car* (*)())dlsym(handle, "create_object");
destroy = (void (*)(Car*))dlsym(handle, "destroy_object");
Car* carr = (Car*)create();
carr->brake();
destroy( carr );
dlclose(handle);
/*
Car carr;
carr.brake();
* compilation g++ test.cpp -o tst /path/libcar.so
*/
return 0;
}
创建 libMyLib.so
并将其安装在 /usr/lib
中后,我尝试使用以下方法编译 test.cc: g++ test.cc -o tst -ldl
。为什么我需要包含 -lMyLib?
有没有办法在没有 libMyLib.so
的情况下编译代码?其次,为什么 dlsym(handle,"brake") 不起作用?如果我用 dlsym(handle,"brake") 更改 dlsym (Car* (*)....
我什么也得不到。为什么?
欣赏
I have:
car.cc
#include "car.h"
#include <iostream>
using namespace std;
extern "C" Car* create_object()
{
return new Car;
}
Car::Car() {
this->maxGear = 2;
this->currentGear = 1;
this->speed = 0;
}
void Car::shift(int gear) {
if (gear < 1 || gear > maxGear) {
return;
}
currentGear = gear;
}
void Car::brake() {
speed -= (5 * this->getCurrentGear());
std::cout<<"THE SPEED IS:" <<speed<<std::endl;
}
extern "C" void destroy_object( Car* object )
{
delete object;
}
car.h
#ifndef VEHICLES_CAR_H
#define VEHICLES_CAR_H
// A very simple car class
class Car {
public:
Car();
void shift(int gear);
void accelerate();
void brake();
private:
int maxGear;
int currentGear;
int speed;
};
#endif /* VEHICLES_CAR_H */
test.cc
#include "/home/car.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
/* on Linux, use "./myclass.so" */
void* handle = dlopen("/usr/lib/libCarTest.so", RTLD_LAZY);
int (*result)(int);
if (!handle)
{
}
/*dlsym(handle,"accelerate");
cout<<"IN HERE: "<<endl;
dlsym(handle,"brake");
dlclose(handle);*/
Car* (*create)();
void (*destroy)(Car*);
dlerror();
create = (Car* (*)())dlsym(handle, "create_object");
destroy = (void (*)(Car*))dlsym(handle, "destroy_object");
Car* carr = (Car*)create();
carr->brake();
destroy( carr );
dlclose(handle);
/*
Car carr;
carr.brake();
* compilation g++ test.cpp -o tst /path/libcar.so
*/
return 0;
}
After creating libMyLib.so
and install it in /usr/lib
i've tried to compile test.cc using: g++ test.cc -o tst -ldl
. WHY do i need to include -lMyLib?
is there a way to compile the code without libMyLib.so
? Secondly why dlsym(handle,"brake")
is not working? If i change dlsym (Car* (*).... with dlsym(handle,"brake")
i get nothing. why?
Appreciate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您需要链接到
Car::brake
方法。因为没有
刹车
符号。方法Car::brake
有一个复杂的损坏的(实现定义的)名称。您可以在 nm -D 的输出中看到这一点。AFAIK,您可以通过
Car
的所有方法设为虚拟(它们将通过指针调用,因此不需要链接)brake()
,该函数将从 .so 调用Car::brake
方法,Car
的所有公共方法inline
并在标头中定义它们。结合最后两种方法:
当然,您可以拆分实现和接口,这样就不会那么混乱。
Because you need to link to the
Car::brake
method.Because there is no
brake
symbol. The methodCar::brake
has a complicated mangled (implementation-defined) name. You can see this in the output ofnm -D
.AFAIK, you can solve it by
Car
virtual (they will be called through a pointer, so no linking will be needed)brake()
that would call theCar::brake
method from the .soCar
inline
and defining them in the header.Combining the last two approaches:
Of course you could split the implementation and the interface so it's not such a mess.