为什么 clang 找不到 unixodbc 函数的符号?
Why doesn't clang find the symbols for unixodbc functions?
我正在尝试在我的 C 程序中使用 unixodbc,并且我包含了使用 odbc 函数所需的 sql.h
头文件。但是,出于某种原因,在尝试编译一个简单的示例文件时,我得到以下输出:
➜ practica2 gcc sale.c
Undefined symbols for architecture x86_64:
"_SQLAllocHandle", referenced from:
_main in sale-179b46.o
"_SQLDescribeCol", referenced from:
_main in sale-179b46.o
"_SQLExecDirect", referenced from:
_main in sale-179b46.o
"_SQLFetch", referenced from:
_main in sale-179b46.o
"_SQLGetData", referenced from:
_main in sale-179b46.o
"_SQLNumResultCols", referenced from:
_main in sale-179b46.o
"_odbc_connect", referenced from:
_main in sale-179b46.o
"_odbc_disconnect", referenced from:
_main in sale-179b46.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
应该是odbc提供的功能,不知道为什么找不到。我使用 Homebrew 安装了 unixodbc,我是 运行 OSX 10.13.1
正在将评论转为答案。
你的命令行没有提到你需要这个库,所以编译器没有 link。它会找到 header,因此您可能只需要在命令行上 object(或源)文件之后的 -lodbc
。 Headers 通知编译器。它们与 linker 无关,是 linker 在抱怨。
因此,在您的示例中,您应该能够编译 link 并使用:
gcc -o sale sale.c -lodbc
并且,为了那些不熟悉 Mac 的人的利益,鉴于您使用的是 Mac,您的 gcc sale.c
命令行确实使用 clang
而不是真正的 GNU gcc
— /usr/bin/gcc
实际上是对 clang
编译器的引用。
$ /usr/bin/gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin17.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$
如果您发现有必要指定 header 在何处使用 -I/opt/unixodbc/include
等选项,那么您还需要指定库在何处使用此类选项作为 -L/opt/unixodbc/lib
.
gcc -o sale -I/opt/unixodbc/include sale.c -L/opt/unixodbc/lib -lodbc
在 Unix-like 系统(Linux、BSD、macOS、AIX、HP-UX、Solaris 等)上,'base location'(/opt/unixodbc
在这个example)变化很大,但通常将 headers 安装在基本位置下的 include
目录中,并将库安装在基本位置下的 lib
目录中。
pkg-config
等工具有时可用于帮助您收集项目所需的不同库集的必要标志。
我正在尝试在我的 C 程序中使用 unixodbc,并且我包含了使用 odbc 函数所需的 sql.h
头文件。但是,出于某种原因,在尝试编译一个简单的示例文件时,我得到以下输出:
➜ practica2 gcc sale.c
Undefined symbols for architecture x86_64:
"_SQLAllocHandle", referenced from:
_main in sale-179b46.o
"_SQLDescribeCol", referenced from:
_main in sale-179b46.o
"_SQLExecDirect", referenced from:
_main in sale-179b46.o
"_SQLFetch", referenced from:
_main in sale-179b46.o
"_SQLGetData", referenced from:
_main in sale-179b46.o
"_SQLNumResultCols", referenced from:
_main in sale-179b46.o
"_odbc_connect", referenced from:
_main in sale-179b46.o
"_odbc_disconnect", referenced from:
_main in sale-179b46.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
应该是odbc提供的功能,不知道为什么找不到。我使用 Homebrew 安装了 unixodbc,我是 运行 OSX 10.13.1
正在将评论转为答案。
你的命令行没有提到你需要这个库,所以编译器没有 link。它会找到 header,因此您可能只需要在命令行上 object(或源)文件之后的 -lodbc
。 Headers 通知编译器。它们与 linker 无关,是 linker 在抱怨。
因此,在您的示例中,您应该能够编译 link 并使用:
gcc -o sale sale.c -lodbc
并且,为了那些不熟悉 Mac 的人的利益,鉴于您使用的是 Mac,您的 gcc sale.c
命令行确实使用 clang
而不是真正的 GNU gcc
— /usr/bin/gcc
实际上是对 clang
编译器的引用。
$ /usr/bin/gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin17.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$
如果您发现有必要指定 header 在何处使用 -I/opt/unixodbc/include
等选项,那么您还需要指定库在何处使用此类选项作为 -L/opt/unixodbc/lib
.
gcc -o sale -I/opt/unixodbc/include sale.c -L/opt/unixodbc/lib -lodbc
在 Unix-like 系统(Linux、BSD、macOS、AIX、HP-UX、Solaris 等)上,'base location'(/opt/unixodbc
在这个example)变化很大,但通常将 headers 安装在基本位置下的 include
目录中,并将库安装在基本位置下的 lib
目录中。
pkg-config
等工具有时可用于帮助您收集项目所需的不同库集的必要标志。