《FreeTDS庫文件之C++代碼中的簡(jiǎn)單應(yīng)用》
??在安裝FreeTDS庫文件之前陨晶,我查閱了好多的文章,講述的都是freetds-0.9x的一些老版本的安裝珍逸。而沒有找到關(guān)于freetds最新版本的安裝說明的。我想大概是我搜索方式不對(duì)吧。無奈只能不斷摸索撮躁,最終將GitHub上下載的最新版本的FreeTDS源代碼編譯成功了。
??寫本文的當(dāng)前時(shí)間為2023年2月15日把曼,GitHub上freetds的源碼鏈接:https://github.com/FreeTDS/freetds。目前發(fā)布的最新的Releases版本是1.3.17嗤军。
??新版本的源碼和舊版本的源碼最大的區(qū)別在于沒有了現(xiàn)成的configure和Makefile文件,因此當(dāng)我們下載了最新的freetds項(xiàng)目源碼解壓后老客,一下子就懵了。感覺查閱過的資料都是騙人的胧砰,說好的configure文件呢?我們查閱的資料包括官網(wǎng)尉间,都在說,運(yùn)行configure文件生成Makefile文件哲嘲,然后make & make install 就安裝成功了,多么簡(jiǎn)單啊眠副。實(shí)際,對(duì)小白的我來說侦啸,configure文件都沒有,還談什么編譯和安裝光涂,只有懵逼相伴。
??好了钝计,扯了一堆,其實(shí)都是我自己的感受私恬,我對(duì)于Linux也是僅限于用過幾個(gè)命令的層次而已炼吴。對(duì)于小白的我在這種情況下也只能默默的看官網(wǎng)的說明本鸣,用百度翻譯一句一句的翻譯硅蹦,過程相當(dāng)難受。好在最終成功編譯了出來童芹。
-
編譯FreeTDS項(xiàng)目源碼需要的環(huán)境
FreeTDS官網(wǎng)的User Guide用戶指南第2章:Build FreeTDS(http://www.freetds.org/userguide/build.html) 中,介紹了
[4] Versions used for this release
* autoconf (GNU Autoconf) 2.69
* automake (GNU automake) 1.15
* ltmain.sh (GNU libtool) 2.4.6
即要想編譯源碼假褪,Linux環(huán)境需要安裝有GNU版本的autoconf、automake宁否、libtool工具。所以第一步就是安裝這些程序:
[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
驗(yàn)證中 : perl-Data-Dumper-2.145-3.el7.x86_64 1/3
驗(yàn)證中 : m4-1.4.16-10.el7.x86_64 2/3
驗(yàn)證中 : 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]# autoconf --version
autoconf (GNU Autoconf) 2.69
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+/Autoconf: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>, <http://gnu.org/licenses/exceptions.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David J. MacKenzie and Akim Demaille.
[root@localhost freetds]#
[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
驗(yàn)證中 : perl-Test-Harness-3.28-3.el7.noarch 1/2
驗(yàn)證中 : 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]# automake --version
automake (GNU automake) 1.13.4
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Tom Tromey <tromey@redhat.com>
and Alexandre Duret-Lutz <adl@gnu.org>.
[root@localhost freetds]#
[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
驗(yàn)證中 : libtool-2.4.2-22.el7_3.x86_64 1/1
已安裝:
libtool.x86_64 0:2.4.2-22.el7_3
完畢冤寿!
[root@localhost freetds]# libtool --version
libtool (GNU libtool) 2.4.2
Written by Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[root@localhost freetds]#
用上面的安裝的版本也是可以編譯成功的青伤,當(dāng)然你也可以指定安裝推薦的版本。
-
下載FreeTDS項(xiàng)目源碼
這里用git工具克隆項(xiàng)目源碼到本地倉庫狠角。首先登陸GitHub,在搜索框中鍵入freetds丰歌,點(diǎn)擊搜索就可以找到FreeTDS項(xiàng)目,打開倉庫FreeTDS/freetds(https://github.com/FreeTDS/freetds)立帖,點(diǎn)擊綠色code按鈕,選擇HTTPS頁堂飞,可以得到代碼鏈接:https://github.com/FreeTDS/freetds.git。
-
用git命令克隆項(xiàng)目源碼到本地路徑
git工具是Linux系統(tǒng)自帶的工具绰筛,可以直接使用描融。
命令:git init myfreetds : 創(chuàng)建一個(gè)本地git倉庫铝噩,名字為myfreetds窿克,當(dāng)然名字可以自己起,效果就是在當(dāng)前目錄下生成一個(gè)myfreetds名字的文件夾让歼,里面有構(gòu)成倉庫的組成文件丽啡,都是隱藏文件。
命令: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
接收對(duì)象中: 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]#
現(xiàn)在源碼就下載好了辈挂,代碼下載還是有點(diǎn)耗時(shí)的,請(qǐng)耐心等待终蒂。如果出現(xiàn)下載失敗的情況,重新下載就可以了拇泣。
-
編譯源代碼并安裝
由于沒有configure和Makefile文件,所以我們無法進(jìn)行編譯霉翔,于是想configure文件應(yīng)該是由其它的執(zhí)行文件生成的,但是不知道應(yīng)該執(zhí)行哪個(gè)文件债朵。這些就要說些經(jīng)驗(yàn)了,我發(fā)現(xiàn)里面有一個(gè)autogen.sh文件序芦,我之前做嵌入式的時(shí)候,也是在Linux環(huán)境下編譯代碼的芝加,生成的可執(zhí)行文件就是以.sh為后綴的。于是我想應(yīng)該是這個(gè)文件了将塑。打開查看一下發(fā)現(xiàn)
#!/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.
srcdir=`dirname $0`
PKG_NAME="FreeTDS."
# If autoreconf encounters an error, it might be because this is the
# very first time it was run, meaning that some files e.g. config.sub
# are missing. We retry with --install (and perhaps we should
# try --force, too).
#
# Revision 1.6 was the last one not to use autoreconf. If you can't get
# this (simpler) one to work, you might try that one.
( cd $srcdir
echo running `which autoreconf` in `pwd`:
autoreconf || autoreconf --install
) || exit
#conf_flags="--enable-maintainer-mode --enable-compile-warnings" #--enable-iso-c
if test x$NOCONFIGURE = x; then
echo Running $srcdir/configure $conf_flags "$@" '...' \
| tr ' ' \\n \
| sed 's/^-/ &/'
$srcdir/configure $conf_flags "$@" \
&& echo Now type \`make\' to compile $PKG_NAME
else
echo Skipping configure process.
fi
"autogen.sh" 44L, 1576C 1,1 全部
文中第一句 “Run this to generate all the initial makefiles, etc.” 意思是“運(yùn)行該文件以生成所有初始makefile等蝌麸。”来吩,那應(yīng)該就是這個(gè)文件了,文章接下的 “From automake.info” 中大概的意思是弟疆,許多軟件包都附帶一個(gè)名為“bootstrap.sh”或“autogen.sh”的腳本,他們會(huì)按正確的順序調(diào)用“aclocal”怠苔、“l(fā)ibtoolize”、“gettextize”或“autopoint”柑司、“autoconf”、“autoheader”和“automake”攒驰。這大大簡(jiǎn)化程序編譯和配置的操作。
那大概流程應(yīng)該就是:
- 首先運(yùn)行 autogen.sh 生成 初始的 configure & Makefile
- 然后通過 configure 配置自定義的 Makefile 文件
- 最后 make & make install
我在運(yùn)行autogen.sh文件的時(shí)候隅津,會(huì)出現(xiàn)一個(gè)報(bào)錯(cuò):
configure.ac:123: warning: macro 'AM_ICONV' not found in library诬垂,
這個(gè)錯(cuò)誤和文中提到的gettextize工具有關(guān)饥瓷,因此我們需要先安裝工具gettext程序。
安裝命令: yum -y install gettext gettext-devel
安裝到后面還有一個(gè)報(bào)錯(cuò):
configure: error: required program 'gperf' not found.
所以我們還需要安裝程序 gperf.
安裝命令:yum -y install gperf
如果你在安裝過程中沒有出現(xiàn)這些錯(cuò)誤呢铆,可以直接跳過,看后面的內(nèi)容棺克。
下面讓我們運(yùn)行 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]# ^C
[root@localhost freetds]#
如果出現(xiàn)上面最后一行:Now type `make' to compile FreeTDS.娜谊,恭喜你确买,你已經(jīng)成功生成了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]#
此時(shí)生成的Makefile文件為默認(rèn)的,初始的配置文件派草。因此我們還需要使用configure來配置我們自定義的Makfile文件。
根據(jù)官網(wǎng)的介紹近迁,configure命令參數(shù)有很多,這里提三個(gè):
- --prefix=PREFIX :設(shè)置FreeTDS庫文件安裝的目錄歧譬,默認(rèn)為:/usr/local,命令行中將PREFIX替換為自定義的路徑瑰步。
- --with-tdsver=VER :指定FreeTDS的安裝版本,默認(rèn)為:auto面氓,命令行中將VER替換為指定的版本號(hào)。這里的版本主要是根據(jù)不通的數(shù)據(jù)庫版本來選擇的,具體什么版本的freetds支持什么數(shù)據(jù)庫可以查閱官網(wǎng)《用戶指南》中第一章中 History of TDS Versions章節(jié) (http://www.freetds.org/userguide/tdshistory.html)掘譬。具體支持配置的版本有哪些,可以通過執(zhí)行./configure --help命令來查看葱轩。下面會(huì)說怎么看藐握。
- --enable-msdblib :這個(gè)選項(xiàng)按官網(wǎng)的解釋不太好理解垃喊,其它資料解釋為:允許Microsoft數(shù)據(jù)庫函數(shù)庫。
查看 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]...
......
>>>>>>>>>>>>>> 中間的內(nèi)容省略 <<<<<<<<<<<<<<
......
--with-tdsver=VERSION TDS protocol version (5.0/7.1/7.2/7.3/7.4/auto)
[auto]
......
>>>>>>>>>>>>>> 中間的內(nèi)容省略 <<<<<<<<<<<<<<
......
Report bugs to the package provider.
[root@localhost freetds]#
可以看到當(dāng)前可以指定的版本為 5.0/7.1/7.2/7.3/7.4,最高為7.4版本乌助。由于我的數(shù)據(jù)庫安裝的是SQL Server2017 所以需要指定最高版本。
下面使用 configure 配置 Makefile 文件他托,這里我指定安裝路徑為默認(rèn)路徑,版本指定7.4赏参,允許使用微軟數(shù)據(jù)庫函數(shù)庫。
命令:./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
......
>>>>>>>>>>>>>> 中間的內(nèi)容省略 <<<<<<<<<<<<<<
......
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
......
>>>>>>>>>>>>>> 中間的內(nèi)容省略 <<<<<<<<<<<<<<
......
config.status: executing libtool commands
[root@localhost freetds]#
此時(shí)新的自定義的Makefile就生成了纫溃。
編譯并安裝
直接運(yùn)行: make 命令進(jìn)行編譯纸俭,然后運(yùn)行 make install命令進(jìn)行安裝即可皇耗,這里不在累贅揍很。
需要說明的是,如果你不是通過git命令拉取的代碼窒悔,而是直接下載的 .zip 或 .tar.gz 壓縮包,那么在make和make install的時(shí)候會(huì)報(bào)一個(gè)查看歷史提交記錄失敗的錯(cuò)誤简珠。如下
git log -1 '--pretty=format:%n<!ENTITY ug.dat......省略......
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
這些錯(cuò)誤一般都出現(xiàn)在最后,此時(shí)所有的庫都編譯或是安裝完成了聋庵,git需要查看提交的歷史記錄,但是你并沒有本地倉庫祭玉,也就沒有提交的歷史記錄,所以失敗了(我的理解)脱货。這些錯(cuò)誤不影響freetds的編譯和安裝律姨,已經(jīng)親自測(cè)試了臼疫。
安裝完成后會(huì)在/usr/local/include目錄下生成頭文件,其中有我們代碼調(diào)用時(shí)需要的sybfront.h和sybdb.h頭文件烫堤,以及編譯時(shí)需要鏈接的庫文件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]#