Linux系统上编译安装FreeTDS库文件

如果你已经成功安装 F r e e T D S 库文件,想了解在代码中如何使用的话,请查阅: \color{red}{如果你已经成功安装FreeTDS库文件,想了解在代码中如何使用的话,请查阅:} 如果你已经成功安装FreeTDS库文件,想了解在代码中如何使用的话,请查阅:《FreeTDS库文件在C++代码中的简单应用》 ,这里提供了一个简单的 D e m o 程序供参考 \color{red}{,这里提供了一个简单的Demo程序供参考} ,这里提供了一个简单的Demo程序供参考

在安装FreeTDS库文件之前,我查阅了好多的文章,讲述的都是 freetds-0.9x 的一些老版本的安装。而没有找到关于 freetds 最新版本的安装说明。我想大概是我搜索方式不对吧。无奈只能不断摸索,最终将从 GitHub 上下载的最新版本的 FreeTDS 源代码编译成功了。

写本文的当前时间为2023年2月15日,GitHubfreetds 的源码链接:https://github.com/FreeTDS/freetds。目前发布的最新的 Releases 版本是1.3.17。

新版本的源码和旧版本的源码最大的区别在于没有了现成的 configureMakefile 文件,因此当我们下载了最新的 freetds 项目源码解压后,一下子就懵了。感觉查阅过的资料都是骗人的,说好的configure文件呢?我查阅的资料包括官网,都在说,运行 configure 文件生成 Makefile 文件,然后 make & make install 就安装成功了,多么简单啊。实际,对于菜鸟的我来说,configure 文件都没有,还谈什么编译和安装,只有懵逼相伴。

好了,扯了一堆,其实都是我自己的感受,我对于 Linux 也是仅限于用过几个命令的层次而已。我也只能默默的看官网的说明,过程很难受。好在最终成功编译了出来。

编译FreeTDS项目源码需要的环境

FreeTDS 官网的 User Guide 用户指南第2章:Build FreeTDS 中,介绍了

[4] Versions used for this release
* autoconf (GNU Autoconf) 2.69
* automake (GNU automake) 1.15
* ltmain.sh (GNU libtool) 2.4.6

即,要想编译源码,Linux 环境需要安装有 GNU 版本的 autoconfautomakelibtool 工具。所以第一步就是安装这些程序:

1. 安装 autoconf

[root@localhost freetds]# yum install autoconf
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
>>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<<
Running transaction
  正在安装    : m4-1.4.16-10.el7.x86_64                                              1/3 
  正在安装    : perl-Data-Dumper-2.145-3.el7.x86_64                                  2/3 
  正在安装    : autoconf-2.69-11.el7.noarch                                          3/3 
  验证中      : perl-Data-Dumper-2.145-3.el7.x86_64                                  1/3 
  验证中      : m4-1.4.16-10.el7.x86_64                                              2/3 
  验证中      : autoconf-2.69-11.el7.noarch                                          3/3 

已安装:
  autoconf.noarch 0:2.69-11.el7

作为依赖被安装:
  m4.x86_64 0:1.4.16-10.el7                                                         perl-Data-Dumper.x86_64 0:2.145-3.el7
完毕!
[root@localhost freetds]# 

2. 安装 automake

[root@localhost freetds]# yum install automake
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
>>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<<
Running transaction
  正在安装    : perl-Test-Harness-3.28-3.el7.noarch                                  1/2 
  正在安装    : automake-1.13.4-3.el7.noarch                                         2/2 
  验证中      : perl-Test-Harness-3.28-3.el7.noarch                                  1/2 
  验证中      : automake-1.13.4-3.el7.noarch                                         2/2 

已安装:
  automake.noarch 0:1.13.4-3.el7

作为依赖被安装:
  perl-Test-Harness.noarch 0:3.28-3.el7

完毕!
[root@localhost freetds]# 

3. 安装 libtool

[root@localhost freetds]# yum install libtool
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
>>>>>>>>>>>>>> 中间的安装过程省略 <<<<<<<<<<<<<<
Running transaction
  正在安装    : libtool-2.4.2-22.el7_3.x86_64                                        1/1 
  验证中      : libtool-2.4.2-22.el7_3.x86_64                                        1/1

已安装:
  libtool.x86_64 0:2.4.2-22.el7_3

完毕!
[root@localhost freetds]# 

用上面的安装的版本也是可以编译成功的,当然你也可以指定安装推荐的版本。

下载FreeTDS项目源码

这里用 git 工具克隆项目源码到本地仓库。项目源码的 https 链接获取可以参考文章《CMake在BC-Linux服务器上源码编译和安装》,这里直接给出代码链接:https://github.com/FreeTDS/freetds.git

1. 用git命令克隆项目源码到本地路径

git 工具是 Linux 系统自带的工具,可以直接使用。如果没有安装,可以使用命令:yum install git 进行安装。

命令1:git init myfreetds      # 创建一个本地git仓库,名字为myfreetds,当然名字可以自己起,效果就是在当前目录下生成一个 myfreetds 名字的文件夹,里面有构成仓库的组成文件,都是隐藏文件。

命令2:git clone https://github.com/FreeTDS/freetds.git      # 意思是拉取GitHub上的源代码到本地。效果就是下载了freetds源代码到myfreetds文件夹中。

如下所示:

[root@localhost code]# git init myfreetds
初始化空的 Git 版本库于 /home/fenghx/code/downtest/myfreetds/.git/
[root@localhost code]# ls
myfreetds
[root@localhost code]# cd myfreetds/
[root@localhost myfreetds]# ls
[root@localhost myfreetds]# ls -al
总用量 0
drwxr-xr-x. 3 root root  18 2月  15 16:13 .
drwxr-xr-x. 3 root root  23 2月  15 16:13 ..
drwxr-xr-x. 7 root root 119 2月  15 16:13 .git
[root@localhost myfreetds]# git clone https://github.com/FreeTDS/freetds.git
正克隆到 'freetds'...
remote: Enumerating objects: 58058, done.
remote: Counting objects: 100% (1286/1286), done.
remote: Compressing objects: 100% (501/501), done.
remote: Total 58058 (delta 927), reused 1098 (delta 785), pack-reused 56772
接收对象中: 100% (58058/58058), 14.51 MiB | 33.00 KiB/s, done.
处理 delta 中: 100% (48275/48275), done.
[root@localhost myfreetds]# ls
freetds
[root@localhost myfreetds]# cd freetds/
[root@localhost freetds]# ls
AUTHORS.md  ChangeLog       config.rpath  COPYING_LIB.txt  doc           freetds.spec.in  INSTALL.GIT.md  interfaces    m4           misc           NEWS.md   PWD.in     README.Windows  src      Thanks-0.95  TODO.freddy  vms
autogen.sh  CMakeLists.txt  configure.ac  COPYING.txt      freetds.conf  include          INSTALL.md      locales.conf  Makefile.am  mkinstalldirs  phptests  README.md  samples         tds.dox  Thanks-1.0   TODO.md      win32
[root@localhost freetds]# 

现在源码就下载好了,代码下载还是有点耗时的,请耐心等待。如果出现下载失败的情况,重新下载就可以了。

编译项目源码

1. 构建项目

由于没有 configure 文件,所以我们无法进行构建项目,此时需要先运行 autogen.sh 文件来生成 configureMakefile 文件。用 vim 工具打开 autogen.sh 文件可以看到如下信息:

#!/bin/sh
# Run this to generate all the initial makefiles, etc.

# $Id: autogen.sh,v 1.10 2011-03-22 17:54:04 jklowden Exp $

# From automake.info:
#
#    Many packages come with a script called `bootstrap.sh' or
# `autogen.sh', that will just call `aclocal', `libtoolize', `gettextize'
# or `autopoint', `autoconf', `autoheader', and `automake' in the right
# order.  Actually this is precisely what `autoreconf' can do for you.
# If your package has such a `bootstrap.sh' or `autogen.sh' script,
# consider using `autoreconf'.  That should simplify its logic a lot
# (less things to maintain, yum!), it's even likely you will not need the
# script anymore, and more to the point you will not call `aclocal'
# directly anymore.

>>>>>>>>>>>>>> 部分文件内容省略 <<<<<<<<<<<<<<

文中第一句 “Run this to generate all the initial makefiles, etc.” 意思是:运行该文件以生成所有初始 makefile 等。文章接下来的 “From automake.info” 中大概的意思是,许多软件包都附带一个名为 bootstrap.shautogen.sh 的脚本,他们会按正确的顺序调用 aclocallibtoolizegettextizeautopointautoconfautoheaderautomake。这大大简化程序编译和配置的操作。

编译流程说明

  • 首先运行 autogen.sh 生成 初始的 configureMakefile 文件
  • 然后通过 configure 配置自定义的 Makefile 文件
  • 最后 make 编译, make install 安装

编译错误说明

在运行 autogen.sh 脚本文件的时候,会出现一个报错
configure.ac:123: warning: macro ‘AM_ICONV’ not found in library
这个错误和文中提到的 gettextize 工具有关,因此我们需要先安装工具 gettext 程序。

安装命令: yum -y install gettext gettext-devel

安装到后面还有一个报错
configure: error: required program ‘gperf’ not found.
所以我们还需要安装 gperf 软件包。

安装命令:yum -y install gperf

以上这两个软件包如果 yum 自己的软件仓库中没有的话,可以从 EPEL 扩展包中安装。EPEL 扩展包的安装详见:《Linux上安装EPEL扩展包》

如果你在安装过程中没有出现这些错误,可以直接跳过,看后面的内容。

运行 autogen.sh

[root@localhost freetds]# ./autogen.sh
running /usr/bin/autoreconf in /home/fenghx/code/myfreetds/freetds:
......
>>>>>>>>>>>>>> 中间的构建过程省略 <<<<<<<<<<<<<<
......
Running
./configure
...
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
......
>>>>>>>>>>>>>> 中间的构建过程省略 <<<<<<<<<<<<<<
......
configure: debug enabled
checking CFLAGS for gcc -Wdeclaration-after-statement... -Wdeclaration-after-statement
done
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating include/freetds/version.h
config.status: creating include/tds_sysdep_public.h
config.status: creating include/freetds/sysdep_types.h
config.status: creating Makefile
config.status: creating include/Makefile
......
>>>>>>>>>>>>>> 中间的构建过程省略 <<<<<<<<<<<<<<
......
config.status: executing depfiles commands
config.status: executing libtool commands
Now type `make' to compile FreeTDS.
[root@localhost freetds]# 

如果出现上面最后一行:Now type `make’ to compile FreeTDS.,恭喜你,你已经成功生成了 configure 文件和 Makefile 文件。如下:

[root@localhost freetds]# ls
aclocal.m4  autom4te.cache  compile       config.rpath   configure        COPYING.txt  freetds.conf     include         install-sh  locales.conf  Makefile     misc           NEWS.md   README.md       src          Thanks-0.95  TODO.md
AUTHORS.md  ChangeLog       config.guess  config.status  configure.ac     depcomp      freetds.spec     INSTALL.GIT.md  interfaces  ltmain.sh     Makefile.am  missing        phptests  README.Windows  tds.dox      Thanks-1.0   vms
autogen.sh  CMakeLists.txt  config.log    config.sub     COPYING_LIB.txt  doc          freetds.spec.in  INSTALL.md      libtool     m4            Makefile.in  mkinstalldirs  PWD.in    samples         test-driver  TODO.freddy  win32
[root@localhost freetds]# 

此时生成的 Makefile 文件为默认的,初始的配置文件。因此我们还需要使用 configure 来配置我们自定义的 Makfile 文件。

配置自定义的 Makefile

根据官网的介绍,configure 命令参数有很多,这里提三个:

  • –prefix=PREFIX :设置 FreeTDS 库文件安装的目录,默认为:/usr/local,命令行中将 PREFIX 替换为自定义的路径。
  • –with-tdsver=VER :指定 FreeTDS 的安装版本,默认为:auto,命令行中将 VER 替换为指定的版本号。这里的版本主要是根据不同的数据库版本来选择的,具体什么版本的 freetds 支持什么版本的数据库可以查阅官网 《用户指南》 中第一章中 《History of TDS Versions》章节。具体支持配置的版本有哪些,可以通过执行 ./configure --help 命令来查看。
  • –enable-msdblib :这个选项按官网的解释不太好理解,其它资料解释为:允许 Microsoft 数据库函数库。

查看 FreeTDS 支持哪些版本的指定,如下:

[root@localhost freetds]# ./configure --help
`configure' configures FreeTDS 1.4.dev.20230215 to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

......
>>>>>>>>>>>>>> 中间的内容省略 <<<<<<<<<<<<<<
......
  --with-tdsver=VERSION   TDS protocol version (5.0/7.1/7.2/7.3/7.4/auto)
                          [auto]
 
......
>>>>>>>>>>>>>> 中间的内容省略 <<<<<<<<<<<<<<
......
Report bugs to the package provider.
[root@localhost freetds]#

可以看到当前可以指定的版本为 5.0/7.1/7.2/7.3/7.4,最高为 7.4 版本。由于我的数据库安装的是 SQL Server2017 所以需要指定最高版本。

下面使用 configure 配置 Makefile 文件,这里我指定安装路径为默认路径,版本指定7.4,允许使用微软数据库函数库。

命令:./configure --prefix=/usr/local --with-tdsver=7.4 --enable-msdblib

[root@localhost freetds]# ./configure --prefix=/usr/local --with-tdsver=7.4 --enable-msdblib
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
......
>>>>>>>>>>>>>> 中间的内容省略 <<<<<<<<<<<<<<
......
configure: debug enabled
checking CFLAGS for gcc -Wdeclaration-after-statement... -Wdeclaration-after-statement
done
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating include/freetds/version.h
config.status: creating include/tds_sysdep_public.h
config.status: creating include/freetds/sysdep_types.h
config.status: creating Makefile
config.status: creating include/Makefile
......
>>>>>>>>>>>>>> 中间的内容省略 <<<<<<<<<<<<<<
......
config.status: executing libtool commands
[root@localhost freetds]# 

到这里,新的自定义的 Makefile 就生成了。

编译源代码并安装

直接运行: make 命令进行编译,然后运行 make install 命令进行安装即可,这里不在累赘。

需要说明的是:如果你不是通过 git 命令拉取的代码,而是直接下载的 .tar.gz 压缩包,那么在 makemake install 的时候会报一个查看历史提交记录失败的错误。如下
git log -1 '–pretty=format:<!-- Autogenerated -->%n<!ENTITY ug.dat…省略…
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
这些错误一般都出现在最后,此时所有的库都编译或是安装完成了,git 需要查看提交的历史记录,但是你并没有本地仓库,也就没有提交的历史记录,所以失败了(我的理解)。这个错误不影响 freetds 的编译和安装,已经亲自测试了

安装完成后会在 /usr/local/include 目录下生成头文件,其中就有开发项目时代码调用需要的 sybfront.hsybdb.h 头文件,以及编译时需要链接的库文件 sybdb

[root@localhost freetds]# ls /usr/local/lib
libct.a  libct.la  libct.so  libct.so.4  libct.so.4.0.0  libsybdb.a  libsybdb.la  libsybdb.so  libsybdb.so.5  libsybdb.so.5.1.0
[root@localhost freetds]# ls /usr/local/include/
bkpublic.h  cspublic.h  cstypes.h  ctpublic.h  odbcss.h  sqldb.h  sqlfront.h  sybdb.h  syberror.h  sybfront.h  tds_sysdep_public.h
[root@localhost freetds]# 

到这里开发软件所需的 FreeTDS 库文件就安装完成了。


如果你已经成功安装 F r e e T D S 库文件,想了解在代码中如何使用的话,请查阅: \color{red}{如果你已经成功安装FreeTDS库文件,想了解在代码中如何使用的话,请查阅:} 如果你已经成功安装FreeTDS库文件,想了解在代码中如何使用的话,请查阅:《FreeTDS库文件在C++代码中的简单应用》 ,这里提供了一个简单的 D e m o 程序供参考 \color{red}{,这里提供了一个简单的Demo程序供参考} ,这里提供了一个简单的Demo程序供参考

好了,FreeTDS 库文件就安装就分享到这里,感谢您的查阅,希望能帮到你。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枫叶2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值