How to create a makefile for Linux / OSX that requires the clock_gettime function

When I tried to create a project on Linux, I got Error: undefined symbol clock_gettime . So I realized that I need to add -lrt to the build (gcc) command. However, now it will not compile on OS X: ld: library not found for -lrt . I don’t know exactly where this function is called called in statically linked code, but it seemed to work fine in OS X without librt. The linked code probably uses the alternative behind #if __APPLE__ or something.

Is there a way I can tell gcc to only link librt if necessary, or if it exists? If not, how do I create a makefile with OS specific commands? I do not use autoconf or anything like that.

The Makefile is quite complex, but the operational 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 
+4
source share
2 answers

You can try the following:

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

If you have a problem with Google, you will find a good set of solutions, one posted as a comment on the question, the other here:

Here

gettimeofday() may be a betteer solution, if you are compiling code that does not clock_gettime you, remember that the clock_gettime function clock_gettime NOT provided in OS X, and you need to change the code.

Hope this can help you, pedr0

-2
source

Source: https://habr.com/ru/post/1437409/


All Articles