简体   繁体   English

Linux上的gcc ODBC未链接

[英]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). 我正在尝试使用一些简单的C odbc代码启动并运行(考虑到需要一个更大的应用程序,因此需要一个相当简单的实用程序)。 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: 这段代码来自easysoft网站,是关于它们可用的最简单的示例:

#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 我的编译语句是: gcc -lodbc listdrivers.c -o listdrivers

Output from gcc: 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 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 | ldconfig -p | grep odbc 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 | readelf -Ws /usr/lib/x86_64-linux-gnu/libodbc.so | grep Alloc 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. 我也曾尝试删除并重新安装unixodbc和unixodbc-dev软件包,但无济于事。 Any help would be greatly appreciated as this is now driving me crazy. 任何帮助将不胜感激,因为这现在使我发疯。

So it turns out that on Ubuntu, the -lodbc needs to come after the c file. 因此,事实证明,在Ubuntu上,-lodbc需要在c文件之后。 So the following appears to work fine: 因此,以下内容似乎可以正常工作:

gcc listdrivers.c -o listdrivers -lodbc 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) 在这种情况下,我没有意识到顺序会有所不同,在Fedora上,顺序也没关系(可能是gcc的其他版本,但我没有具体检查)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM