Skip to content
Advertisement

Linker cannot find local shared library

I’m trying a very simple exmaple to create a shared library and link to it. The shared library is as follows:

#ifndef ARDUGRAB_H_
#define ARDUGRAB_H_

#include <iostream>

using namespace std;

namespace ArduGrabLibrary{

    class ArduGrab{
    public:
            ArduGrab();
            virtual void initCamera();
            virtual void setSim(bool sim);
            virtual void setDebug(bool debug);
    private:
            bool debug = false;
            bool sim = false;
    };
}

Then the source code file is just as simple:

#include "ardugrab.h"


namespace ArduGrabLibrary
{

    ArduGrab::ArduGrab(){
            std::cout << "IMX298 Constructor" << std::endl;
    }

    void ArduGrab::initCamera(){
            if (this->debug){
                    cout << "init camera" << std::endl;
            }
    }

    void ArduGrab::setSim(bool sim){
            this->sim = sim;
            if (this->debug){
                    cout << "set sim to " << std::boolalpha << this->sim << std::endl;
            }
    }

    void ArduGrab::setDebug(bool debug){
            this->debug = debug;
            if (this->debug){
                    cout << "set debug to " << std::boolalpha << this->sim << std::endl;
            }
    }
}

I’m then compiling that into a shared library with:

g++ -fPIC -shared -o ardugrab.so ardugrab.cpp

All good, we get an ardgrab.so library so to test it, with the following code in teh same directory as the .so and .h files from above:

#include "ardugrab.h"

using namespace ArduGrabLibrary;

int main() {
    std::cout << "starting program" << std::endl;
    ArduGrab* ardu = new ArduGrab();
    ardu->setDebug(true);
    //imx298->setSim(true);
    //imx298->initCamera();
    return 0;
}

So now we need to compile this into an executable with:

g++ -L. -lardugrab -o testardugrab testardugrab.cpp

This however fails to find the ardugrab.so file, the follow error message appears:

pi@raspberrypi:~/ArduMipiGrab $ g++ -L. -lardugrab -o testardugrab testardugrab.cpp
/usr/bin/ld: cannot find -lardugrab
collect2: error: ld returned 1 exit status

I’ve tried setting LD_LIBRARY_PATH to . export LD_LIBRARY_PATH=. but still nothing.

As you can see I’m a bit new with compiling c++, can someone please advise me as to what I’m doing wrong?

Thanks.

Reagrds,

Neil

Advertisement

Answer

This is becuase you are using the -l flag.

When you use this flag (Rather than specify a library specifically) it assumes a certain naming convention.

-lX

The linker assumes the file name is

libX.so (or libX.a)

So the commands you want are:

> g++ -fPIC -shared -o libardugrab.so ardugrab.cpp
> #                    ^^^
> g++ -L. -lardugrab -o testardugrab testardugrab.cpp

Note: The environment variable LD_LIBRARY_PATH is used at runtime when the standard library tries to find and load required shared libraries. I.E. it is not used during compilation to find shared libraries to link with.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement