C++, can't access included header files from so library

2.1k Views Asked by At

I try to include my self built .so library in the test.cpp file. When I try to make the test.cpp file I get this exception:

root@airdrop:/home/pi/naza-interface/examples# make
g++ -c test.cpp
test.cpp:31:35: fatal error: naza_interface_manual.h: No such file or 
directory
#include "naza_interface_manual.h"
                               ^
compilation terminated.
Makefile:5: recipe for target 'test.o' failed
make: *** [test.o] Error 1

The Makefile for test.cpp looks like that:

output: test.o
            g++ -L. -lnazainterface -o test test.o

test.o:
            g++ -c test.cpp

clean:
            rm -f *.o

test.cpp just includes the library.

#include "naza_interface_manual.h"

The library contains two files, naza_interface_manual.h and naza_interface_manual.cpp. The library's makefile looks like that:

libso: naza_interface_manual.o pca9685.o
            g++ -fPIC -L/usr/local/lib -shared naza_interface_manual.o 
            pca9685.o -lbcm2835 -o libnazainterface.so

naza_interface_manual.o: src/naza_interface_manual.cpp src/naza_interface_manual.h
            g++ -fPIC -c -Wall src/naza_interface_manual.cpp

pca9685.o: src/PCA9685/pca9685.cpp src/PCA9685/pca9685.h
            g++ -c src/PCA9685/pca9685.cpp

install: naza_interface_manual.o pca9685.o
            g++ -L/usr/local/lib naza_interface_manual.o pca9685.o -lbcm2835 - 
shared -o /usr/local/libnazainterface.so

clean:
            rm *.o output

naza_interface_manual.h:

#ifndef NAZA_INTERFACE_MANUAL_H_
#define NAZA_INTERFACE_MANUAL_H_


class naza_interface_manual_c{
    public:
            naza_interface_manual_c();

            // A: For roll control (left/right)
            // E: For pitch control (front/back)
            // T: For throttle control
            // R: For rudder control
            // U: For Control Model Switch
            void configure_pins(int A, int E, int T, int R, int U);

            void fly_forward(int speed);
            void fly_backward(int speed);
            void fly_up(int speed);
            void fly_down(int speed);
            void fly_left(int speed);
            void fly_right(int speed);

 };


 #endif

naza_interface_manual.cpp:

#include <iostream>
#include <wiringPi.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

#include "naza_interface_manual.h"
#include "PCA9685/pca9685.h"

naza_interface_manual_c::naza_interface_manual_c(){

    std::cout << "Starting Naza Interface";

}

void naza_interface_manual_c::configure_pins(int A, int E, int T, int R, int U){
    PCA9685 pca9685;

    pca9685.SetFrequency(100);

    pca9685.Write(CHANNEL(0), VALUE(350));
}
void naza_interface_manual_c::fly_forward(int speed){

}
void naza_interface_manual_c::fly_backward(int speed){

}
void naza_interface_manual_c::fly_up(int speed){

}
void naza_interface_manual_c::fly_down(int speed){

}
void naza_interface_manual_c::fly_left(int speed){

}
void naza_interface_manual_c::fly_right(int speed){

}
2

There are 2 best solutions below

2
Brian Cain On BEST ANSWER

Your compilation doesn't give the compiler the include path to find the header.

Instead, specify a base location and add the path to the compile. Otherwise if you can change the naza interface library, its install target should install the headers to a system (or $PREFIX/include) location.

test.o:
            g++ -I$(NAZA_INTERFACE_LIB)/src/ -c test.cpp
2
ravnsgaard On

Your Makefile doesn't install the header file. In fact, it also installs the shared object in a non-standard location: /usr/local. You want the library to go into /usr/local/lib and you need the header file installed in /usr/local/include.

Your Makefile is not consistent with conventional rules: You have no all rule, you are creating the library directly in the installation directory, instead of calling /usr/bin/install... I suggest you look into "proper" Makefile layout, if you want to distribute this. Users expect a lot of things from the Makefiles you give them; there are de-facto standards to follow.

If you want to use the library without having installed it, you need to provide the compiler the relevant include directive in your test.o: target; something like -Ipath/to/your/header.