5

When I tried to build a project in Linux, I got Error: undefined symbol clock_gettime. So I figured out that I needed to add -lrt to the build command (gcc). However, now it won't compile in OS X: ld: library not found for -lrt. I don't know exactly where this function is getting called as it's in statically linked code, but it seemed to be working just fine in OS X without librt. The linked code probably uses an alternative behind #if __APPLE__ or something.

Is there any way that I can instruct gcc to only link librt if it's needed, or if it exists? If not, how do I create a Makefile with OS-specific commands? I'm not using autoconf or anything like it.

The Makefile is rather complex, but here's the operative part:

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
meustrus
  • 5,075
  • 4
  • 35
  • 48
  • could you post the makefile if is it simple ? – pedr0 Oct 02 '12 at 15:02
  • see this: http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x – pedr0 Oct 02 '12 at 15:03
  • If anybody else tries to tell me about alternatives to `clock_gettime` I'm going to strangle somebody. I went to great pain to say I didn't control using this function, and this is purely a compiler question, not a code question. (the rage comes from the results of my preliminary google search entirely answering the "alternatives to this function" question) – meustrus Oct 02 '12 at 15:12

2 Answers2

12

You could try this:

LFLAGS := -lpthread

OS := $(shell uname -s)
ifeq ($(OS),Linux)
LFLAGS += -lrt
endif
Beta
  • 86,746
  • 10
  • 132
  • 141
-1

If you google the problem you will find a good set of solutions, one is posted as a comment at the question, another is here:

Here

gettimeofday() could be a betteer solution, if you are compiling code not writed by you bear in mind that clock_gettime function is NOT provided under OS X and you need to modify the code.

Hope that can help you, pedr0

pedr0
  • 2,682
  • 5
  • 28
  • 44