LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Makefile: including a subdirectory filled with headers and srcs (http://www.linuxquestions.org/questions/linux-newbie-8/makefile-including-a-subdirectory-filled-with-headers-and-srcs-754159/)

chris24300 09-10-2009 10:06 AM

Makefile: including a subdirectory filled with headers and srcs
 
Hi, I'm not familiar with makefile at all and I need to compile some test code to communicate with a Phidgets board. I know of the -I option so I've been trying to get that to work but I have no luck. In my directory I have the phidgets folder that contains the headers and sources to their calls. I tried looking at makefile templates but they were far too confusing. Here is my makefile

Code:

# Phidgets makefile

all: test

CC        = gcc

INCL        = /phidget21

test: test.c
        $(CC) test.c -o test -I$(INCL)

//////////////////////////////////////////
                OUTPUT
//////////////////////////////////////////

gcc test.c -o test -I/phidget21
/tmp/ccMlFdVS.o: In function `main':
test.c:(.text+0xee): undefined reference to `CPhidgetInterfaceKit_create'
test.c:(.text+0x103): undefined reference to `CPhidget_open'
test.c:(.text+0x119): undefined reference to `CPhidget_getDeviceStatus'
test.c:(.text+0x141): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x15e): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x17b): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x198): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x1b5): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
/tmp/ccMlFdVS.o:test.c:(.text+0x1d2): more undefined references to `CPhidgetInterfaceKit_getSensorValue' follow
collect2: ld returned 1 exit status
make: *** [test] Error 1


Thanks for all the help!
Chris

GrapefruiTgirl 09-10-2009 10:10 AM

The -l or -L options are linker arguments, as well as (or rather than) compiler arguments.

If you want to tell the linker to include a shared (object) library, use the lowercase -l with the path to/name of the library. To tell the linker to include a directory full of libraries in its search path, use the capital -L with the path to the directory.

Examples:

-lsomelib.so
-L/usr/lib

Best success,
Sasha

chris24300 09-10-2009 10:56 AM

Thanks for the response GrapefruiTgirl. The folder looks like it was included but I still get the same output. I don't understand why I still get an undefined reference, the files are clearly in the phidgets directory

Code:

ls ./phidget21/
clog.c                      cphidgetencoder.o      cphidgetmanager.o            cphidgettextlcd.c      cusblinux.c      pdict-impl.h
clog.h                      cphidgetgps.c          cphidgetmotorcontrol.c      cphidgettextlcd.h      cusblinux.o      pdict.o
clog.o                      cphidgetgps.h          cphidgetmotorcontrol.h      cphidgettextlcd.o      cusbmac.c        pdictserver.c
cphidgetaccelerometer.c      cphidgetgps.o          cphidgetmotorcontrol.o      cphidgettextled.c      cusbwindows.c    pdictserver.h
cphidgetaccelerometer.h      cphidgetgyroscope.c    cphidget.o                  cphidgettextled.h      dns_sd.h        phidget21.c
cphidgetaccelerometer.o      cphidgetgyroscope.h    cphidgetphsensor.c          cphidgettextled.o      examples        phidget21.h
cphidgetadvancedservo.c      cphidgetgyroscope.o    cphidgetphsensor.h          cphidgetweightsensor.c  hotplug          phidget21int.h
cphidgetadvancedservo.h      cphidget.h              cphidgetphsensor.o          cphidgetweightsensor.h  Java            phidget21.o
cphidgetadvancedservo.o      cphidgetinterfacekit.c  cphidgetrfid.c              cphidgetweightsensor.o  libphidget21.a  stdafx.c
cphidgetattr.h              cphidgetinterfacekit.h  cphidgetrfid.h              csocketevents.c        libphidget21.so  stdafx.h
cphidget.c                  cphidgetinterfacekit.o  cphidgetrfid.o              csocketevents.h        libusb          udev
cphidgetconstants.c          cphidgetled.c          cphidgetservo.c              csocketevents.o        Makefile        utils
cphidgetconstants.h          cphidgetled.h          cphidgetservo.h              csocket.h              md5.c            zeroconf_avahi.c
cphidgetconstantsinternal.h  cphidgetled.o          cphidgetservo.o              csocketopen.c          md5.h            zeroconf_avahi.o
cphidgetconstants.o          cphidgetlist.c          cphidgetstepper.c            csocketopen.o          md5.o            zeroconf.c
cphidgetdictionary.c        cphidgetlist.h          cphidgetstepper.h            cthread.c              pdict.c          zeroconf.h
cphidgetdictionary.h        cphidgetlist.o          cphidgetstepper.o            cthread.h              pdictclient.c
cphidgetdictionary.o        cphidgetmacros.h        cphidgettemperaturesensor.c  cthread.o              pdictclient.h
cphidgetencoder.c            cphidgetmanager.c      cphidgettemperaturesensor.h  cusbce.c                pdictclient.o
cphidgetencoder.h            cphidgetmanager.h      cphidgettemperaturesensor.o  cusb.h                  pdict.h


GrapefruiTgirl 09-10-2009 10:58 AM

Hmmm.. Did you happen to check that CPhidgetInterfaceKit_getSensorValue is actually defined in one of the header files?

Oh, wait now! Maybe... Just maybe... the way you are doing your INCLUDE there is like:

Code:

gcc test.c -o test -I/phidget21
and:
Code:

INCL        = /phidget21
Both of which are absolute paths. Maybe try using a relative path, or if the folder is right there within the source, just remove the leading slash on the folder name, OR, use the full absolute path, like:

gcc test.c -o test -Iphidget21

or

gcc test.c -o test -I/full/path/to/location/of/phidget21

:) maybe we just missed an obvious, very simple typo/thinko?

Sasha

PS - my apology too -- earlier, I misread your capital I (as in 'eye') to be an L (as in ELephant) -- lol, fonts are tricky that way at times.

GrapefruiTgirl 09-10-2009 11:08 AM

Plus, to make it more bulletproof, I would use something like:

gcc test.c -o test -I${PWD}/phidget21

which will translsate to "current-working-directory"/"phidget21"

Sasha

chris24300 09-10-2009 12:23 PM

Nope, still same output...

I'm 100% positive it's defined in there, and I get undefined reference for _create, open, and getdevicestatus which are defined in the source files I looked at. Maybe I should just put the file in the phidgets directory?

Thanks again!
Chris

That didn't work either... Should I post the code from test.c? Maybe theres a format issue or something.

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "phidget21.h"

CPhidgetInterfaceKitHandle ifkit;

int main(int argc, char **argv)
{
    int phidgetIkStatus = 0;
    int input1 = 0;
    int input2 = 0;
    int input4 = 0; // added front sensor
    int input5 = 0; // added front sensor
    int input6 = 0; // added front sensor
    int input7 = 0; // added front sensor
    ////////////////////////////////////////////////

    // attempt to open the phidgets interface kit
    CPhidgetInterfaceKit_create(&ifkit);
    CPhidget_open((CPhidgetHandle)ifkit, -1);
   
    // prepare to read the information from the phidgets interface board
    CPhidget_getDeviceStatus((CPhidgetHandle)ifkit, &phidgetIkStatus);
   
    if (phidgetIkStatus != 0)
    {
            // read the IR values
        CPhidgetInterfaceKit_getSensorValue(ifkit, 1, &input1);
        CPhidgetInterfaceKit_getSensorValue(ifkit, 2, &input2);
        CPhidgetInterfaceKit_getSensorValue(ifkit, 4, &input4);
        CPhidgetInterfaceKit_getSensorValue(ifkit, 5, &input5);
        CPhidgetInterfaceKit_getSensorValue(ifkit, 6, &input6);
        CPhidgetInterfaceKit_getSensorValue(ifkit, 7, &input7);

        printf("%d\n%d\n%d\n%d\n%d\n%d", input1, input2, input4, input5, input6, input7); fflush(stdout);
    } // if
   
} // main


chris24300 09-11-2009 01:12 PM

I'm still trying to figure this out, can someone tell me why the initial error comes from /tmp/ccKRuoUK.o? I have the header file in the subdirectory and all the functions are defined. Here is the output

Code:

gcc -o test test.c -I${PWD}/phidget21
/tmp/ccKRuoUk.o: In function `main':
test.c:(.text+0xee): undefined reference to `CPhidgetInterfaceKit_create'
test.c:(.text+0x103): undefined reference to `CPhidget_open'
test.c:(.text+0x119): undefined reference to `CPhidget_getDeviceStatus'
test.c:(.text+0x141): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x15e): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x17b): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x198): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
test.c:(.text+0x1b5): undefined reference to `CPhidgetInterfaceKit_getSensorValue'
/tmp/ccKRuoUk.o:test.c:(.text+0x1d2): more undefined references to `CPhidgetInterfaceKit_getSensorValue' follow
collect2: ld returned 1 exit status


GrapefruiTgirl 09-11-2009 02:00 PM

http://gcc.gnu.org/onlinedocs/gcc-3.0.2/cpp_2.html

Review section 2.1 at the above link. After that, make sure that your location of the mysterious header file, is being correctly included as per the directives on that C preprocessor page.

For example, let's say you have SOURCE, which is a folder from which you are executing your gcc command. And inside SOURCE, you have your PHIDGITS folder, which is where this mystery header file is located.

In this case, I think your #include would read:

#include "PHIDGITS/phidgits.h"

See where I'm leading with this?

And conversely, if the file "phidgits.h" is located right there in the same folder as your command is being run (the SOURCE folder) then your #include would be as you have it:

#include "phidgits.h"

Hopefully I'm not repeating something you've already tried earlier -- I'm no C guru by any stretch of the imagination, just trying to eliminate the easy possibilities ;)

Sasha


All times are GMT -5. The time now is 09:39 PM.