我想将我的项目链接到一个静态库 (msodbcsql11.lib),我想使用一个简单的 makefile 包含它的头文件 (msodbcsql.h) 和框架 otlv4.h 的另一个头文件,但它看起来就像找不到图书馆一样。这是我的生成文件:
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
标志仅告诉链接器到哪里寻找库,您需要使用 -l
告诉它要链接到哪个库。
关于c++ - 使用 makefile 将静态库与 OTL 链接时 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28897264/