Ryan Bazinet
Ryan Bazinet

Reputation: 51

gcc ODBC on Linux is not linking

I am attempting to get up and running with some simple C odbc code (with a fairly simple utility in mind that I need for a larger application). The problem is, I am so far unable to get my simple test case to compile and link into a binary that I can actually execute.

This code is from the easysoft website, and is about the simplest example they have available:

#include <stdio.h>
#include <sql.h>
#include <sqlext.h>

main() {
  SQLHENV env;
  char driver[256];
  char attr[256];
  SQLSMALLINT driver_ret;
  SQLSMALLINT attr_ret;
  SQLUSMALLINT direction;
  SQLRETURN ret;

  SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
  SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);

  direction = SQL_FETCH_FIRST;
  while(SQL_SUCCEEDED(ret = SQLDrivers(env, direction,
                       driver, sizeof(driver), &driver_ret,
                       attr, sizeof(attr), &attr_ret))) {
    direction = SQL_FETCH_NEXT;
    printf("%s - %s\n", driver, attr);
    if (ret == SQL_SUCCESS_WITH_INFO) printf("\tdata truncation\n");
  }
}

My compile statement is: gcc -lodbc listdrivers.c -o listdrivers

Output from gcc:

/tmp/cchgAMyC.o: In function `main':
listdrivers.c:(.text+0x2f): undefined reference to `SQLAllocHandle'
listdrivers.c:(.text+0x4d): undefined reference to `SQLSetEnvAttr'
listdrivers.c:(.text+0xd8): undefined reference to `SQLDrivers'
collect2: error: ld returned 1 exit status

I thought maybe it wasn't actually finding the library, so I specified the path on the command line:

gcc -lodbc -L/usr/lib/x86_64-linux-gnu listdrivers.c -o listdrivers

Still no luck. I also verified that the driver is installed and configured for use.

ldconfig -p | grep odbc

libodbcinstQ4.so.1 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libodbcinstQ4.so.1
libodbcinst.so.2 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libodbcinst.so.2
libodbcinst.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libodbcinst.so
libodbccr.so.2 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libodbccr.so.2
libodbccr.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libodbccr.so
libodbc.so.2 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libodbc.so.2
libodbc.so (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libodbc.so
libiodbcinst.so.2 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libiodbcinst.so.2
libiodbcadm.so.2 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libiodbcadm.so.2
libiodbc.so.2 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libiodbc.so.2

Then I thought maybe the library itself is corrupt, but that also looks fine:

readelf -Ws /usr/lib/x86_64-linux-gnu/libodbc.so | grep Alloc

   113: 0000000000007870    21 FUNC    GLOBAL DEFAULT   11 SQLAllocStmt
   148: 0000000000007820    67 FUNC    GLOBAL DEFAULT   11 SQLAllocHandleStd
   164: 0000000000007810    10 FUNC    GLOBAL DEFAULT   11 SQLAllocHandle
   196: 0000000000006720    21 FUNC    GLOBAL DEFAULT   11 SQLAllocConnect
   213: 0000000000006740    20 FUNC    GLOBAL DEFAULT   11 SQLAllocEnv

The function is clearly listed in the library, and I see no reason why that library would not be getting included, but at this point, I am stuck...

I have also tried removing and reinstalling the unixodbc and unixodbc-dev packages to no avail. Any help would be greatly appreciated as this is now driving me crazy.

Upvotes: 1

Views: 5962

Answers (1)

Ryan Bazinet
Ryan Bazinet

Reputation: 51

So it turns out that on Ubuntu, the -lodbc needs to come after the c file. So the following appears to work fine:

gcc listdrivers.c -o listdrivers -lodbc

I didn't realize the order would make any difference in this case, and on Fedora the order doesn't matter (possibly a different version of gcc, but I didn't check specifically)

Upvotes: 4

Related Questions