任何有關以下查詢的建議將不勝感激。我已經嘗試了一切,但仍然對這個錯誤感到震驚。該庫 (FastPFor) 與 Visual Studio 解決方案配合良好;當我嘗試將它與包含檔案 (src/example.cpp) 的專案一起使用時,就會出現問題。
我正在嘗試將靜態庫 (lib/FastPFor/libFastPFOR.a) 鏈接到我的 example.cpp。我已經復制了 (include/FastPFor/*h) 中的頭檔案,同時我通過運行CMake命令后跟make, 在通過 WSL 運行的 Ubuntu 上獲得了 FastPForlib.a。
以下是我的依賴樹
example (proj)
include
------------>FastPFor
--------------------->*h
lib
------------>FastPFor
--------------------->liblibFastPFor.a
src
------------>example.src
Makefile
以下是我的makefile:
.SUFFIXES:
#
.SUFFIXES: .cpp .o .c .h
LDFLAGS = -Llib/FastPFor -llibFastPFOR
# replace the CXX variable with a path to a C 11 compatible compiler.
# to build an aligned version, add -DUSE_ALIGNED=1
ifeq ($(INTEL), 1)
# if you wish to use the Intel compiler, please do "make INTEL=1".
CXX ?= /opt/intel/bin/icpc
CC ?= /opt/intel/bin/icpc
ifeq ($(DEBUG),1)
CXXFLAGS = -fpic -std=c 11 -O3 -Wall -ansi -xAVX -DDEBUG=1 -D_GLIBCXX_DEBUG -ggdb
CCFLAGS = -fpic -std=c99 -O3 -Wall -ansi -xAVX -DDEBUG=1 -D_GLIBCXX_DEBUG -ggdb
else
CXXFLAGS = -fpic -std=c 11 -O2 -Wall -ansi -xAVX -DNDEBUG=1 -ggdb
CCFLAGS = -fpic -std=c99 -O2 -Wall -ansi -xAVX -DNDEBUG=1 -ggdb
endif # debug
else #intel
CXX ?= g -4.7
ifeq ($(DEBUG),1)
CXXFLAGS = -fpic -mavx -std=c 11 -Weffc -pedantic -ggdb -DDEBUG=1 -D_GLIBCXX_DEBUG -Wall -Wextra -Wextra -Wsign-compare -Wwrite-strings -Wpointer-arith -Winit-self -Wno-sign-conversion
CCFLAGS = -fpic -mavx -std=c99 -pedantic -ggdb -DDEBUG=1 -D_GLIBCXX_DEBUG -Wall -Wextra -Wsign-compare -Wwrite-strings -Wpointer-arith -Winit-self -Wno-sign-conversion
else
CXXFLAGS = -fpic -mavx -std=c 11 -Weffc -pedantic -O3 -Wall -Wextra -Wsign-compare -Wwrite-strings -Wpointer-arith -Winit-self -Wno-sign-conversion
CCFLAGS = -fpic -mavx -std=c99 -pedantic -O3 -Wall -Wextra -Wsign-compare -Wwrite-strings -Wpointer-arith -Winit-self -Wno-sign-conversion
endif #debug
endif #intel
HEADERS= $(shell ls include/FastPFor/*.h)
all: example
example: $(HEADERS) src/example.cpp
$(CXX) $(CXXFLAGS) -o example $(LDFLAGS) src/example.cpp
clean:
rm -f *.o example
astyle:
astyle --options=astyle.conf --recursive "*.cpp" "*.h"
.PHONY: all clean astyle
運行make產生了一堆錯誤,都與未定義的 FastPFOR 參考有關。它看起來像下面這樣:
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x781): undefined reference to `__fastunpack11(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x791): undefined reference to `__fastunpack10(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x7a1): undefined reference to `__fastunpack9(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x7b1): undefined reference to `__fastunpack4(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x7c1): undefined reference to `__fastunpack3(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x7d1): undefined reference to `__fastunpack64(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x7e1): undefined reference to `__fastunpack8(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x7f1): undefined reference to `__fastunpack7(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x801): undefined reference to `__fastunpack6(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x811): undefined reference to `__fastunpack5(unsigned int const*, unsigned long*)'
/usr/bin/ld: example.cpp:(.text._ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm[_ZN11FastPForLib12FastPForImplILj8EmE13__decodeArrayEPKjRmPmm] 0x87c): undefined reference to `__fastunpack2(unsigned int const*, unsigned long*)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:36: example] Error 1
先感謝您。
uj5u.com熱心網友回復:
首先,在提問時,請始終包含在您收到錯誤之前列印出來的命令(在本例中為鏈接行)。
問題是您在鏈接線上放置東西的順序。 -llibFastPFOR它本身不是一個選項,它告訴聯結器查看該檔案并包含任何需要但當前未定義的符號。但是由于您將它放在鏈接行的第一個example.cpp位置,在 之前,還不需要任何符號(除了main)。
您必須首先將源(和物件)檔案放在鏈接行上,在庫之前。因此,將聯結器命令更改為:
$(CXX) $(CXXFLAGS) -o example src/example.cpp $(LDFLAGS)
這在很多方面都不是一個非常標準/行為良好的 makefile,但是這種更改應該可以解決您的直接問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/521002.html
