0

Let me start by saying that the reason I'm using this model is because this particular specimen old and relatively disposable, which is appropriate for my project.

I have written myself a library and am now trying to link against it. To install it I do the whole git clone and run sudo make install, which creates a 'libmylibrary.a' archive in /usr/local/lib and copies the header to /usr/local/include. I have verified that the make/install process seems to be working correctly on both platforms, with the outputs in the right place and the list of symbols the same using nm.

I am now trying to create a program to test it, but it's refusing to compile on the Raspberry Pi platform with errors like undefined reference to `MyClass::MyClass(int,int)` (depending on which member I try to use). I understand this is a problem at linking stage. It's obviously finding the library as it should, but I don't understand why it's resolving the symbol on one platform and not the other.

I'm compiling with g++ test.cpp -o test -llgpio -lmylibrary. Although it's not included in the example code below, the real mylibrary does depend on lgpio.

The test program is simple, something like:

#include<iostream>
#include<lgpio.h>
#include<mylibrary.h>

int main() {
    std::cout << "Initialising..." << std::endl;
    
    int num1 = library1function();
    
    std::cout << testarr[3] << std::endl;
    
    // Linking on Rasperry Pi is successful if I comment the next two lines out:
    MyClass obj(1,2); 
    std::cout << obj.test() << std::endl; 
    
    std::cout << "done" << std::endl;
}

mylibrary.h is also what you would expect:

#ifndef mylibrary
#define mylibrary

#include<lgpio.h>

const int testarr[] = {1, 2, 3, 4, 5};

class MyClass {
    public:
        MyClass(int a, int b);
        MyClass(int a, int b, int c);
        
        int test();
        
    private:
        int _privateInt;
};
#endif

and the original mylibrary.cpp consists of:

#include "mylibrary.h"

MyClass::MyClass(int a, int b) : MyClass(a, b, 1) {};
MyClass::MyClass(int a, int b, int c) {
    _privateInt = a * b * c;
}

int MyClass::test() {
        return _privateInt;
}

The actual library is of course much more complicated that this and does depend on lgpio but I couldn't think of a way to include that in this simplified example above. I did the development on Ubuntu and then started running into these problems once I started trying to actually use it on the Raspberry Pi end. The test program compiles and runs without issues on the former, but I have the linking problem on Raspberry Pi.

1 Answer 1

0

Turns out it was an issue with the makefile causing one source in the library to be compiled twice instead of compiling two different files.

Still unsure why this was only happening on Raspberry Pi and not on the Ubuntu PC - they are both running GNU MAKE 4.3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.