qed
qed

Reputation: 23104

How to use a dynamic lib in eclipse?

Here is an small example I did with clang++ :

===filename===
calc_mean.cpp
===filename===



===filecontent===


double mean(double a, double b) {
  return (a+b) / 2;
}


===filecontent===





===filename===
calc_mean.h
===filename===



===filecontent===


double mean(double, double);


===filecontent===





===filename===
commands.sh
===filename===



===filecontent===


#/usr/bin/env bash

clang++ -c calc_mean.cpp -o calc_mean.o
ar rcs libmean.a calc_mean.o
clang++ -c -fPIC calc_mean.cpp -o calc_mean.o
gcc -shared -W1,-soname,libmean.so.1 -o libmean.so.1.0.1  calc_mean.o
clang++ main.cpp -L. -lmean -o dynamicmain -v


===filecontent===





===filename===
main.cpp
===filename===



===filecontent===


#include <stdio.h>
#include "calc_mean.h"

int main(int argc, char const* argv[])
{
    double v1, v2, m;
    v1 = 5.0;
    v2 = 6.0;
    m = mean(v1, v2);
    printf("Mean: %f\n", m);
    return 0;
}


===filecontent===

It worked perfectly. Now turn to eclipse, I created a project with the dynamic lib generated above in the libs folder:

Alt text
(source: p.im9.eu)

Adjusted -L and -l settings accordingly:

Alt text
(source: p.im9.eu)

Got these errors:

Alt text
(source: p.im9.eu)

Other things I have tried:

Alt text
(source: p.im9.eu)

Alt text
(source: p.im9.eu)

The errors stayed the same. I almost want to bang my head against a wall now. Should I start learning cmake already?

update

I added the header file also this time, but eclipse still can't resolve the function mean (through code analysis). It compiles without an error though, but when I run the output binary, it says:

dyld: Library not loaded: libmean.so
  Referenced from: /Users/kaiyin/personal_config_bin_files/workspace/testuselib/Debug/testuselib
  Reason: image not found

Upvotes: 1

Views: 545

Answers (1)

Caladain
Caladain

Reputation: 4934

Edit2: It hit me that you're on Mac, and I remembered that there's something funny about library loading. So, there are a couple reasons why you'd get Image Not Found. The below still applies, but there's another reason it could be failing. See dyld: Library not loaded: libqscintilla2.5.dylib

I don't know if eclipse on Mac even ships with GCC, or if it's clang only on that platform, but try setting DYLD_LIBRARY_PATH as a quick test to see if it's just Mac Being Special. https://superuser.com/questions/282450/where-do-i-set-dyld-library-path-on-mac-os-x-and-is-it-a-good-idea

Edit: Yay it compiles! Now we're hitting a linking error. This one is actually pretty fun, and isn't the "common" one I listed below (namely, Unresolved Symbols). This error, "Image Not Found" usually means that the Linker found the library, but could not use it because it was compiled in an incompatible manner.

Why is it in any incompatible format? Welcome to the one feature of C++ that I hate is missing, and one of the reasons pretty much every library out there provides a C interface instead of a C++ interface.

C++ Does Not Provide a stable ABI (Application Binary Interface). This means that libraries compiled with different compilers (or even just different versions of the same compiler may not work together. 99/100 they will just outright refuse to link/work, but even if they do link, you'll get very weird, hard-to-impossible to track down bugs, etc.

Here's the tl;dr: If you want your static lib to be C++ (which i recommend) and have a C++ interface, you need to make sure the exact same version of the compiler is used to compile both your application and the static library. The easiest way to do this is to have eclipse build both the static library and the application.

This is hopefully changing with the next version of C++, as Herb Sutter has put forward a proposal to create a platform defined C++ ABI.

Original: You need to add the folder containing calc_mean.h to the "Additional Includes" for c++ generation. You can think of include statement as cutting and pasting the contents of the file at that exact line. The error is saying "hey, i went looking for a file called calc_mean.h and couldn't find it." You need to link the library and the header (so main.cpp knows the function)

If it was an error saying "unresolved symbols", with the symbols being in your library, then you would know you've messed up with adding the library or library path (-L).

Cmake is a good tool, but it is nice to know how to use an ide. The basic steps (add library name, add library path, add directory containing library headers) are the same in eclipse, netbeans, visual studio, xcode, etc)

Upvotes: 1

Related Questions