使用Autotools为iPhoneOS编译FreeTDS的Golang实现

使用Autotools为iPhoneOS编译FreeTDS的Golang实现 希望我的问题发在了正确的论坛,如果发错了地方,我表示歉意!

我正在尝试编译 FreeTDS,以便在 iOS 应用中使用。我需要使用 Autotools,但我对它完全陌生,感觉有点不知所措。以下是我尝试过的内容:

我编写了一个 sh 脚本并运行它:

#!/bin/sh

 # unset some shell variables

#./configure --build is used for specifing the architecture you want to complie for
#./configure --host is used to specify the ark of the machine doing the compileing (running xcode)
#./configure --target seems to be an alias

#uname -p to get system architecture

VERSION="1.3"
SDKVERSION="15"
TARGETSDKVERSION="12"
LIB="freetds"

DEVELOPER=`xcode-select -print-path`
ARCH="arm64"
CURRENTPATH=`pwd`
HOST="i386-apple-darwin21.2.0"
OLD_PATH=$PATH
TDS_VER=7.4

 cd freetds-1.3

 SDK="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk"

 unset CC
 unset CXXFLAGS
 unset CFLAGS
 unset LDFLAGS
 unset LP
 unset CPP

 export buildPath=`pwd`


 # make arm target
 #export CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2
 export CC="clang"
 export CFLAGS="-arch ${ARCH} -miphoneos-version-min=${TARGETSDKVERSION} -isysroot=${SDK}"
 export CPP=/usr/bin/cpp
 export CXXFLAGS="$CFLAGS"
 export LDFLAGS="$CFLAGS"
 export LD=$CC

 ./configure --build=aarch64-apple-darwin21.2.0 --host=i386-apple-darwin21.2.0 --target=arm64 --with-tdsver=7.4 --with-libiconv-prefix=/Users/yannik/opt/anaconda3

 make clean
 make uninstall

但是,我遇到了错误: clang: **error:** **linker command failed with exit code 1 (use -v to see invocation)**

我该如何修复这个错误并为 iPhoneOS 编译我的代码?


更多关于使用Autotools为iPhoneOS编译FreeTDS的Golang实现的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于使用Autotools为iPhoneOS编译FreeTDS的Golang实现的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


要使用Autotools为iPhoneOS编译FreeTDS,你需要正确设置交叉编译环境。以下是针对iOS编译的配置示例:

#!/bin/sh

# 设置iOS编译环境
export PLATFORM="iPhoneOS"
export SDKVERSION="15.0"
export MIN_VERSION="12.0"
export ARCH="arm64"

DEVELOPER=`xcode-select -print-path`
SDK="${DEVELOPER}/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${SDKVERSION}.sdk"

# 清理环境变量
unset CC
unset CXX
unset CFLAGS
unset CXXFLAGS
unset LDFLAGS
unset CPPFLAGS

# 设置交叉编译工具链
export CC="clang"
export CXX="clang++"

# 设置iOS特定的编译标志
export CFLAGS="-arch ${ARCH} -isysroot ${SDK} -miphoneos-version-min=${MIN_VERSION} -fembed-bitcode"
export CXXFLAGS="${CFLAGS}"
export LDFLAGS="-arch ${ARCH} -isysroot ${SDK} -miphoneos-version-min=${MIN_VERSION}"

# 配置FreeTDS
cd freetds-1.3
./configure \
    --host=arm-apple-darwin \
    --build=x86_64-apple-darwin \
    --disable-shared \
    --enable-static \
    --with-tdsver=7.4 \
    --disable-odbc \
    --disable-apps \
    --disable-server \
    --disable-pool \
    --disable-threadsafe

# 编译
make

对于Golang集成,你需要使用cgo来链接编译好的FreeTDS库。以下是Golang代码示例:

// #cgo CFLAGS: -I/path/to/freetds/include
// #cgo LDFLAGS: -L/path/to/freetds/lib -ltds -liconv -lssl -lcrypto
// #include <sybfront.h>
// #include <sybdb.h>
import "C"

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    // 初始化FreeTDS
    if C.dbinit() != C.SUCCEED {
        panic("Failed to initialize FreeTDS")
    }
    
    // 创建连接句柄
    var login *C.DBPROCESS
    login = C.dblogin()
    if login == nil {
        panic("Failed to create login handle")
    }
    
    // 设置连接参数
    C.DBSETLUSER(login, C.CString("username"))
    C.DBSETLPWD(login, C.CString("password"))
    C.DBSETLAPP(login, C.CString("GoApp"))
    
    // 清理
    C.dbloginfree(login)
    C.dbexit()
}

如果你需要同时支持多个架构(如arm64和x86_64模拟器),可以使用lipo工具合并库文件:

# 编译arm64版本
export ARCH="arm64"
# ... 运行上述配置和编译步骤
cp src/libtds/.libs/libtds.a libtds-arm64.a

# 编译x86_64模拟器版本
export ARCH="x86_64"
export PLATFORM="iPhoneSimulator"
# ... 重新配置和编译
cp src/libtds/.libs/libtds.a libtds-x86_64.a

# 合并库文件
lipo -create libtds-arm64.a libtds-x86_64.a -output libtds-universal.a

确保在Xcode项目中正确设置Header Search Paths和Library Search Paths,指向你编译的FreeTDS头文件和库文件所在目录。

回到顶部