Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build Linux/OSX makefile needing clock_gettime function

Tags:

gcc

makefile

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
like image 428
meustrus Avatar asked Oct 02 '12 15:10

meustrus


1 Answers

You could try this:

LFLAGS := -lpthread

OS := $(shell uname -s)
ifeq ($(OS),Linux)
LFLAGS += -lrt
endif
like image 199
Beta Avatar answered Nov 15 '22 06:11

Beta