0

I have a so file with a list of functions, now I would like to build a C++ program to use those functions of the library

I have tried: g++ -Llibname.so xxx.cpp and set the LD_LIBRARY_PATH environment variable of the library .so already, however it is still giving me an error like function not found.

Could you please give me any idea for compliling C++ program with library so?

Many thanks!

#include <iostream>
#include <signal.hpp>
using namespace std;

int main() {
    cout << "!!!Hello World!!!" << endl;
    processData("/home/radwan/2011-07-22Field1/", 0,
                    4500, 1,
                    100, 70,
                    90, 8,
                    100,1.8);
    return 0;
}

header file declaration:

 int processData(char const * directory, int const minimumSignalLevel,
        int const maximumSignalLevel, int const samplingFactor,
        int const minimumTrackDimension, int const minimumMissingVineStocksDimension,
        int const maximumMissingVineStocksDimension, int const maximumFoliageHoleDimension,
        int const cellSide4Average,float const & FRF_R_threshold_Value);
bluewonder
  • 767
  • 2
  • 10
  • 18

3 Answers3

0

I think you should give the header to the caller, let the caller find it by #include. Every cpp file only find symbols through itself first, if you do not tell it other header info, it will not to search. But if you cc some *.c file, compiler will help find each other, even you do not give header files.

0

LD_LIBRARY_PATH is used to aid run-time finding of the .so file. You can also hardcode some paths in the executable. Anyway, the general format of the compilation line is:

g++ -Ldirectory where the library can be found-lname without the lib prefix or .so extension` xxx.cpp -o xxx

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • when i specified all the path and parameter like you mentioned above, I still have an error of function not found, in my case it is processData() :( – bluewonder Jun 04 '13 at 09:29
  • You're getting a compile time error or link time error? The former could be because the library's header file doesn't have a declaration matching your call. The latter could be because the header file declares something that the .so doesn't contain.... If you're not sure which it is, post the exact error message and any code you can see is relevant (e.g. the header declaration and call). – Tony Delroy Jun 04 '13 at 09:34
  • I had a compile time error, yes I think it might because of the header file, I have one testing file (c++) which has been created by a company that produced a lib (please take a look at the new code I have just added above) As Im new to linux, thank you for helping me out! the error is processData is not found – bluewonder Jun 04 '13 at 09:38
  • Hmmm... interesting. Can you please also post the `processData` function declaration from the `signal.hpp` header file? – Tony Delroy Jun 04 '13 at 09:46
  • @user2076858: when I use that function declaration and your function call, it compiles properly, and I get a "undefined reference to `processData(...` error because I don't have the library to link. If I change the function declaration to be a definition (replace the trailing `;` with `{ }`), it compiles and links cleanly. Could you please post the full, exact text of your error message? – Tony Delroy Jun 04 '13 at 09:57
  • Yes actually, I have fixed like this: I have added all the references to the header files in this cpp file, then after that I use the following command to compile the cpp file "g++ -L/root/Desktop/fa/ -lsignalProcessing.so -I/root/Desktop/fa/ xxx.cpp -o xxx" where -I (I as I am not small l) as the fa directory contains all the header files that I have. After that, I have this error msg, just like I didn't set the standard path for the .so lib "/usr/bin/ld: cannot find -lsignalProcessing.so" – bluewonder Jun 04 '13 at 10:02
  • @user2076858 You just need `-lsignalProcessing` - can remove the ".so" extension. – Tony Delroy Jun 04 '13 at 10:15
0

Note that the order of the libraries and object files may be important. I had a similar problem lately and the solution was to reorder the library switch in the linker commandline.

You can take a look here and see if this might be your problem: Linking a DLL using xerces gives undefined symbols

Community
  • 1
  • 1
Devolus
  • 21,661
  • 13
  • 66
  • 113