【问题标题】:How to build Linux/OSX makefile needing clock_gettime function如何构建需要clock_gettime函数的Linux/OSX makefile
【发布时间】: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 的替代方案,我会勒死某人。我很痛苦地说我没有控制使用这个功能,这纯粹是一个编译器问题,而不是代码问题。 (愤怒来自我初步谷歌搜索的结果,完全回答了“此功能的替代品”问题)

标签: gcc makefile


【解决方案1】:

如果你用谷歌搜索这个问题,你会找到一套很好的解决方案,一个作为评论发布在问题上,另一个在这里:

Here

gettimeofday() 可能是一个更好的解决方案,如果您正在编译不是您编写的代码,请记住在 OS X 下不提供clock_gettime 函数,您需要修改代码。

希望对你有帮助, pedr0

【讨论】:

    【解决方案2】:

    你可以试试这个:

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

    【讨论】:

    • 可能不是最好的方法,但至少它适用于我目前的情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 2017-08-24
    • 1970-01-01
    • 2011-10-11
    • 2016-06-20
    • 2013-05-22
    相关资源
    最近更新 更多