如何构建需要clock_gettime函数的Linux/OSX makefile

meu*_*rus 5 gcc makefile

当我尝试在Linux中构建项目时,我得到了Error: undefined symbol clock_gettime.所以我想出了我需要添加-lrt到构建命令(gcc).但是,现在它不会在OS X中编译:ld: library not found for -lrt.我不知道这个函数究竟在哪里被调用,因为它在静态链接代码中,但它似乎在没有librt的OS X中工作得很好.链接的代码可能使用了替代#if __APPLE__或替代.

是否有任何方法可以指示gcclibrt在需要时链接,或者是否存在?如果没有,如何使用特定于操作系统的命令创建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
Run Code Online (Sandbox Code Playgroud)

Bet*_*eta 12

你可以试试这个:

LFLAGS := -lpthread

OS := $(shell uname -s)
ifeq ($(OS),Linux)
LFLAGS += -lrt
endif
Run Code Online (Sandbox Code Playgroud)