Understanding gcc/unix - Unix

This is a discussion on Understanding gcc/unix - Unix ; Hello all, First of all, my background consists mainly of OS/2. Now I'm trying to start working in unix:ish environments. Primarily NetBSD. I'm having some trouble understanding how to do basic things like compiling and linking. I have written several ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Understanding gcc/unix

  1. Understanding gcc/unix

    Hello all,

    First of all, my background consists mainly of OS/2. Now I'm trying
    to start working in unix:ish environments. Primarily NetBSD.

    I'm having some trouble understanding how to do basic things like
    compiling and linking. I have written several programs which use
    standard libraries, but as soon as I try to use custom libraries, I run
    into problems.

    In a particular case, I'm trying to use apache's apr library. I have
    written a small test:

    ----------------------------------
    #include

    #include
    #include
    #include

    void *thread(apr_thread_t *thrd, void *param)
    {
    apr_sleep(500);
    puts("Yo!");
    apr_sleep(500);
    puts("Yo!");
    apr_sleep(500);

    apr_thread_exit(thrd, 0);
    }

    int main(void)
    {
    apr_status_t rc = 0;
    apr_thread_t *new_thread = NULL;
    apr_pool_t *pool = NULL;

    apr_initialize();
    atexit(apr_terminate);
    apr_pool_create(&pool, NULL);

    rc = apr_thread_create(&new_thread, NULL, thread, NULL, pool);

    apr_sleep(3000);

    return EXIT_SUCCESS;
    }
    ----------------------------------

    I compile (and link) with this:

    $ gcc -L/usr/pkg/lib -lapr-0 -I/usr/pkg/include aprtest.c

    This produces an a.out file. But when I try to run ./a.out, I get this:

    $ ./a.out
    Shared object "libapr-0.so.0" not found

    However:

    $ locate libapr-0.so.0
    /usr/pkg/lib/libapr-0.so.0
    /usr/pkg/lib/libapr-0.so.0.9.7

    In OS/2, I would simply fix my LIBPATH. But I have no idea what I
    need to do in NetBSD to make it work. Any tips?


    Also, I would like to know how to make my program as portable as
    possible in unix:ish environments (with regards to the build system -- I
    already know how to write portable code). I mean, if I write a makefile
    which refers to /usr/pkg/lib/libapr-0.so, that won't be very portable. I
    assume this is what the normally-required "./configure" is for. So what
    is "./configure"? Is it a script that I need to write myself which
    checks what system it is running under, and create the appropriate
    makefile? This seems quite volatile, since even if I determine that the
    program is being built under NetBSD, there's no guarantee that packages
    are installed under /usr/pkg. Or does the script need to check that too?
    That would require quite a big script for all the possible combinations
    of systems out there.. So I get the feeling that there's a much cleaner
    way to do this.

    --
    Kind Regards,
    Jan Danielsson
    Te audire non possum. Musa sapientum fixa est in aure.

  2. Re: Understanding gcc/unix

    Jan Danielsson said:
    > I'm having some trouble understanding how to do basic things like
    >compiling and linking. I have written several programs which use
    >standard libraries, but as soon as I try to use custom libraries, I run
    >into problems.


    > I compile (and link) with this:
    >
    >$ gcc -L/usr/pkg/lib -lapr-0 -I/usr/pkg/include aprtest.c


    Fishy.. you pretty much should be getting linker errors from that;
    the "-lapr-0" should be specified after your source files, not
    before.

    > This produces an a.out file. But when I try to run ./a.out, I get this:
    >
    >$ ./a.out
    >Shared object "libapr-0.so.0" not found
    >
    > However:
    >
    >$ locate libapr-0.so.0
    >/usr/pkg/lib/libapr-0.so.0
    >/usr/pkg/lib/libapr-0.so.0.9.7
    >
    > In OS/2, I would simply fix my LIBPATH. But I have no idea what I
    >need to do in NetBSD to make it work. Any tips?


    One-shot fix is that you add the directory into LD_LIBRARY_PATH
    environment variable. I guess there's some place in NetBSD where
    you could also make a permanent, global, change to the library
    search path, but don't know what that would be.

    > Also, I would like to know how to make my program as portable as
    >possible in unix:ish environments (with regards to the build system -- I
    >already know how to write portable code). I mean, if I write a makefile
    >which refers to /usr/pkg/lib/libapr-0.so, that won't be very portable. I
    >assume this is what the normally-required "./configure" is for. So what
    >is "./configure"?


    Google for autoconf and automake (autotools might also be a viable
    search term). There's quite a huge infrastructure of things behind
    "configure" scripts.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)

+ Reply to Thread