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

时间:2015-03-06 10:53:55

标签: c++ makefile linker g++

我想将我的项目链接到一个静态库(msodbcsql11.lib),我希望使用一个简单的makefile包含它的头文件(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 $@

2 个答案:

答案 0 :(得分:2)

-L标志只告诉链接器在哪里查找库,您需要使用-l告诉它链接到哪个库。

答案 1 :(得分:1)

这可能会有所帮助......

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