使用 makefile 将静态库与 OTL 链接时未定义的引用

undefined references when linking with static library with OTL using a makefile

我想 link 我的项目到一个静态库 (msodbcsql11.lib),我想包含它的头文件 (msodbcsql.h) 和另一个框架的头文件 otlv4.h) 使用一个简单的 makefile,但看起来它找不到库。这是我的生成文件:

CC=g++
LDFLAGS=
CFLAGS=-c -Wall
SOURCES=main.cpp
LIBB = C:\temp\lib
LIBINCL = C:\temp\include
CFLAGS += -I$(LIBINCL)
LDFLAGS += -L$(LIBB)
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=main
all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

这是我的 C++ 代码:

#include <iostream>
#define OTL_ODBC_MSSQL_2008 // Compile OTL 4/ODBC, MS SQL 2008
#include <otlv4.h>

int main(){
    otl_connect db; //undefined reference errors
}

这是我声明时的构建 otl_connect:

18:42:33 **** Incremental Build of configuration Default for project mak ****
make all 
g++ -c -Wall -IC:\temp\include main.cpp -o main.o
g++  -LC:\temp\lib main.o -o main
main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x66): undefined reference to `SQLFreeHandle'
main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x66): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLFreeHandle'
main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x9e): undefined reference to `SQLFreeHandle'
main.o:main.cpp:(.text$_ZN8otl_connD1Ev[_ZN8otl_connD1Ev]+0x9e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLFreeHandle'
main.o:main.cpp:(.text$_ZN8otl_conn6logoffEv[_ZN8otl_conn6logoffEv]+0x54): undefined reference to `SQLDisconnect'
main.o:main.cpp:(.text$_ZN8otl_conn6logoffEv[_ZN8otl_conn6logoffEv]+0x54): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLDisconnect'
main.o:main.cpp:(.text$_ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x70): undefined reference to `SQLGetDiagRec'
main.o:main.cpp:(.text$_ZN8otl_conn5errorER7otl_exc[_ZN8otl_conn5errorER7otl_exc]+0x70): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLGetDiagRec'
main.o:main.cpp:(.text$_ZN8otl_conn6commitEv[_ZN8otl_conn6commitEv]+0x23): undefined reference to `SQLEndTran'
main.o:main.cpp:(.text$_ZN8otl_conn6commitEv[_ZN8otl_conn6commitEv]+0x23): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SQLEndTran'
collect2: error: ld returned 1 exit status
makefile:14: recipe for target 'main' failed
make: *** [main] Error 1

18:42:35 Build Finished (took 1s.993ms)

我的 makefile 有什么问题?我该怎么办?

编辑:我根据@Jonathan Wakely 的回答进行了更新:

LIBB = C:\temp\lib
LIBFILE = msodbcsql11
LDFLAGS += -L$(LIBB) -l$(LIBFILE)

我仍然遇到同样的错误

更新:按照@Jonathan Wakely 的建议更新了 makefile,但仍然出现错误。顺便说一句,我将 libmsodbcsql11.lib 的文件格式更改为 libmsodbcsql11.a 因为编译器无法检测到 .lib 版本

CC=g++
LDFLAGS=
CFLAGS=-c -Wall
SOURCES=main.cpp
LIBB = C:\temp\lib
LIBFILE = msodbcsql11
LIBINCL = C:\temp\include
CFLAGS += -I$(LIBINCL)
OBJECTS=$(SOURCES:.cpp=.o)
LDFLAGS += -L$(LIBB) -l$(LIBFILE)
EXECUTABLE=main

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(OBJECTS) $(LDFLAGS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

-L标志只告诉linker去哪里寻找库,你需要使用-l告诉它link到哪个库。

这可能会有帮助...

显然 OTL library 需要 odbc32.lib 文件来调用 ODBC 函数。这有点令人困惑,因为 Microsoft ODBC 驱动程序有这个 msodbcsql11.lib,显然它不是所需要的。不管我之前版本的makefile没有调用任何库,所以我接受了@Jonathan Wakely的回答。