How to get address of a function in .so library ?

I have a .so which comes from a third partner, I use nm to see the function name I need. When I use dlsym to get the function address, the return value is null.

Below is the function name i need when I use nm and c++filt:
0000000000153da0 T CThostFtdcMdApi::CreateFtdcMdApi(char const*, bool, bool)
00000000001095f0 T CThostFtdcUserApi::CreateFtdcUserApi(char const*, bool, bool)

Below is the code which relates with the function:
string s=getenv("HOME");
pso=dlopen((s+"/LIBRARY/thostmduserapi.so").c_str(), RTLD_LAZY | RTLD_GLOBAL | RTLD_NODELETE); // successfully

pf=(psofunc)dlsym(pso,"CreateFtdcMdApi"); // failure,return NULL

the return error is: undefined symbol: CreateFtdcMdApi

My question is that why can't find it? why can find it by nm ?

by the way, in the header file provided by the third partner, the method CreateFtdcMdApi is declared with static.

Can any one help me ? thanks.
Does the third party describe the interface of the library?
Yes, a few head files are provided. The factory method CreateFtdcMdApi is declared as static, and at out of the class, an external method overload it, like this:

extern "C" CThostFtdcMdApi *CreateFtdcMdApi(const char *Path = "", const bool bIsUsingUdp=false, const bool bIsMulticast=false);

This method is defined in an abstract class named CThostFtdcMdApi. In the class, some virtual methods are defined,too.

In addition to this problem, I just found that all of the methods which privided in the abstract class can't be found when I use dlsym function.

Any ideas about it ?
Last edited on
You are using explicit linking rather than implicit linking. Why?
I use Netbeans to develop my code, so it do everything for me. I just design and code. By the way, what is implicit linking ? This is my first c++ program.

I just try to test the whole functions in that .so file. Only one function can be found, but it is useless.

In addition, I test another small size .so file in linux, some functions can be found.
Last edited on
Lets take a program:
1
2
3
4
5
6
#include <iostream>

int main() {
  std::cout << "Hello\n";
  return 0;
}

Did I use dlopen or dlsym? No.

Does that mean that no shared object (aka dynamic library, e.g. .so) is involved? No.

The compiler creates an object file based on this source code. This code refers to identifier std::cout and operator<<. The operator is a function call. Those two must have been declared within the header file (for we did not declare them), but their implementation is in a library. The object file will contain only references/calls to the external symbols.

The compiler is followed by the linker, which takes object file(s) and libraries and creates an executable. Will the executable have everything in it? No.

The executable has equivalents of dlopen and dlsym, which are executed while we load our program; the necessary libraries are loaded when we start our program, and when we reach the "print hello", functions of the library are called.


The standard libraries that provide those functions are automatically considered by the linker. Your third-party library has to be mentioned for the linker.


1. In source code:
1a) include header(s)
1b) use types and functions declared in header(s)

2. Tell your "does everything" Netbeans to use the "thostmduserapi.so" in your project.
Many thanks to keskiverto, thank you very much !

Well, I'll try to let NetBeans compile and link the code according to your ideas.

Another question about the implicit link, does the linker can link my code with .so library ? I have only two .so file.
Last edited on
After changing to implicit linking, I got an error:
undefined reference to `CreateFtdcMdApi(char const*, bool, bool)'

The compile command is:
g++ -c -g -Wall -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux/QuoteAPI.o.d" -o build/Debug/GNU-Linux/QuoteAPI.o QuoteAPI.cpp

I am paying attention to this problem now.

Thank you very much.
Last edited on
OK, I found a small bug. All code can be compiled and linked together. Thank you.

But a new error occured: when I call the static method CreateFtdcMdApi, the system generates a SIGSEGV error.

Signal received: SIGSEGV (Segmentation fault)

Could you tell me what problem it is ?

The problem has been solved now. A string is wrong.

So many thanks to you.
Last edited on
Topic archived. No new replies allowed.