0

I made a library, and I'm trying to make a test client for it to test my Debian packages. This test is being done on Ubuntu 14.04.

I installed the binary and the developer files and their dependencies.

Here's the source for my test program:

#include <stdio.h>
#include <cquel.h>

int main(int argc, char *argv[])
{
    cq_init(1024, 128);

    struct dbconn mydb = cq_new_connection(u8"pattstest.delwink.com", u8"patts",
            u8"patts", u8"pattsdb");

    struct dlist *users;
    int rc = cq_select_all(mydb, u8"User", &users, u8"");
    if (rc)
        return 2;

    for (size_t i = 0; i < users->fieldc; ++i)
        printf("%s\n", users->fieldnames[i]);

    cq_free_dlist(users);
    return 0;
}

The program is supposed to connect to a test server and get the column headers from the database (no, that server is not production and does not need to be particularly secure).

I attempted to compile using gcc:

$ gcc -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c 
/tmp/ccjd21kP.o: In function `main':
/home/mac/c/main.c:6: undefined reference to `cq_init'
/home/mac/c/main.c:8: undefined reference to `cq_new_connection'
/home/mac/c/main.c:12: undefined reference to `cq_select_all'
/home/mac/c/main.c:19: undefined reference to `cq_free_dlist'
collect2: error: ld returned 1 exit status

I knew something was up, so I attempted the same with clang:

$ clang -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c

Clang compiled just fine! I ran my a.out binary, and it printed the column headers as expected.

Why did gcc fail to link to my library?

EDIT: I thought to check my LD_LIBRARY_PATH to find that it was blank. However, setting it to /usr/lib/x86_64-linux-gnu (which is the location of my shared object) did not change the behavior of gcc.

2mac
  • 1,609
  • 5
  • 20
  • 35

1 Answers1

3

Order of arguments matter a lot for gcc; you should use

   gcc -Wall $(pkg-config --cflags cquel) $(mysql_config --cflags) \
       -std=c11 main.c \
        $(pkg-config --libs cquel) $(mysql_config --libs) 

See also this & that.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • This worked. I knew gcc could be picky about arguments, but I always thought the source file had to go last. `gcc -Wall -std=c11 main.c \`mysql_config --libs --cflags\` \`pkg-config --libs --cflags cquel\`` worked. – 2mac Nov 25 '14 at 17:49
  • 2
    with gcc, at link time, the libraries have to be listed after the object files – user3629249 Nov 25 '14 at 20:07