Chris Chris - 1 month ago 6
C Question

os x swift build package manager cannot find shared library from unixODBC C dependency

On OS X El Capitan, I am attempting to integrate a swift wrapper for unixODBC 2.3.4, but the build fails at the linker step with the following error:

ld: library not found for -lodbc.2 for architecture x86_64

I immediately confirmed that the installed library is in fact 64 bit:

file /usr/local/lib/libodbc.dylib
/usr/local/lib/libodbc.dylib: Mach-O 64-bit dynamically linked shared library x86_64

For my project, I am utilizing the swift package manager, so I have installed the latest developer bits for swift and prepended its bin folder to my
environment variable:

export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:$PATH

I installed unixODBC 2.3.4 successfully using homebrew and created the requisite odbcinst.ini and odbc.ini files to access a postegresql database. I confirmed that the odbc installation and configuration are correct by connecting to a remote database with the isql utility included by unixODBC.

In my CunixODBC swift project I created the following module.modulemap file referencing the newly installed unixODBC shared library

module CunixODBC [system] {
header "/usr/local/Cellar/unixodbc/2.3.4/include/unixodbc_conf.h"
header "/usr/local/Cellar/unixodbc/2.3.4/include/sql.h"
header "/usr/local/Cellar/unixodbc/2.3.4/include/sqltypes.h"
link "odbc"
export *

In my swiftodbc project that establishes the dependency on CunixODBC, my Package.swift file is:

import PackageDescription

let package = Package(
dependencies: [
.Package(url: "../CunixODBC", versions: Version(0,0,4)..<Version(1,0,0))

As additional confirmation that unixODBC is functioning correctly, I wrote a small test program in C and linked to the unixODBC shared library. My compile string is:

cc -L/usr/local/lib -I/usr/local/Cellar/unixodbc/2.3.4/include -lodbc odbctest.c -o odbctest

The compile and link succeed and resulting executable works as expected.

What step or configuration am I missing?


Try adding: -Xlinker -L/usr/local/lib to your swift build instantiation.