2012-10-02 25 views
3

當我嘗試在Linux中構建項目時,得到了Error: undefined symbol clock_gettime。所以我發現我需要將-lrt添加到構建命令(gcc)中。但是,現在它不能在OS X中編譯:ld: library not found for -lrt。我不知道這個函數究竟在哪裏被調用,因爲它是靜態鏈接的代碼,但它似乎在沒有librt的OS X中工作得很好。鏈接的代碼可能使用#if __APPLE__之類的替代方法。如何構建需要clock_gettime函數的Linux/OSX makefile

有沒有什麼辦法可以讓我指導gcc只聯繫librt如果需要的話,還是存在?如果不是,我該如何創建一個具有特定操作系統命令的Makefile?我沒有使用autoconf或類似的東西。

的Makefile文件是相當複雜的,但這裏的執行部分:

CC := g++ 
# In this line, remove -lrt to compile on OS X 
LFLAGS := -lpthread -lrt 
CFLAGS := -c -Wall -Iboost_build -Ilibtorrent_build/include -Iinc 
OBJDIR := obj 
SRCDIR := src 
SRC := $(wildcard $(SRCDIR)/*.cpp) 
OBJS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRC)) 

# Note that libtorrent is built with a modified jamfile to place the 
# libtorrent.a file in a consistent location; otherwise it ends up somewhere 
# dependent on build environment. 
all : $(OBJS) libtorrent_build boost_build 
    $(CC) -o exec $(LFLAGS) \ 
    $(OBJS) \ 
    libtorrent_build/bin/libtorrent.a \ 
    boost_build/stage/lib/libboost_system.a 
+0

你可以張貼makefile文件,如果是很簡單? – pedr0

+0

看到這個:http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x – pedr0

+0

如果有人試圖告訴我有關'clock_gettime'的替代方法,我會扼殺某人。我非常痛苦地說我不能控制使用這個函數,而這純粹是一個編譯問題,而不是代碼問題。 (憤怒來自我的初步谷歌搜索的結果完全回答「替代這個功能」問題) – meustrus

回答

10

你可以試試這個:

LFLAGS := -lpthread 

OS := $(shell uname -s) 
ifeq ($(OS),Linux) 
LFLAGS += -lrt 
endif 
+0

可能不是最好的方式,但至少它適用於我目前的情況。 – meustrus

-2

如果谷歌,你會找到一個很好的一整套解決方案的問題,一個張貼於問題的評論,另一個是在這裏:

Here

gettimeofday()可能是一個更好的解決方案,如果您編譯的代碼沒有被編寫,請記住clock_gettime函數不在OS X下提供,您需要修改代碼。

希望可以幫助你, pedr0