【发布时间】:2012-09-23 11:01:15
【问题描述】:
当我尝试在 Linux 中构建项目时,我得到了Error: undefined symbol clock_gettime。所以我发现我需要将-lrt 添加到构建命令(gcc)中。但是,现在它无法在 OS X 中编译:ld: library not found for -lrt。我不确切知道这个函数在哪里被调用,因为它是在静态链接代码中,但它似乎在没有 librt 的 OS X 中工作得很好。链接的代码可能使用#if __APPLE__ 或其他东西后面的替代方法。
有什么方法可以指示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
【问题讨论】:
-
如果简单的话,你能发布makefile吗?
-
如果有人试图告诉我
clock_gettime的替代方案,我会勒死某人。我很痛苦地说我没有控制使用这个功能,这纯粹是一个编译器问题,而不是代码问题。 (愤怒来自我初步谷歌搜索的结果,完全回答了“此功能的替代品”问题)