How to change the following Makefile so that it works for MacOS? [closed]

雨燕双飞 提交于 2020-01-25 10:18:05

问题


I used to do C programming in Linux. Now,I got MacBook and I want to do programming on the Mac.

How should I change the following makefile ?

# Makefile

OBJS = main

$(OBJS): $(OBJS).o
#   gcc -O2 -Wall $(OBJS).c -o $(OBJS)  -I/usr/local/include  -L/usr/local/lib64   -leggx -lX11 -lm
    gcc $(OBJS).c -o $(OBJS)  -I/usr/local/include  -L/usr/local/lib64   -leggx -lX11 -lm

$(OBJS).o: $(OBJS).c
    gcc -c $(OBJS).c

.PHONY: clean
clean:
     rm -f $(OBJS) $(OBJS).o 

回答1:


You will want to review at least the following:

  • do you want to continue to use gcc as your compiler, or move to Apple's clang

  • if you want to use X11, you will need to install XQuartz and add the following options:

    X11FLAGS=-I /opt/X11/include X11LIBS=-L /opt/X11/lib -lx11

  • you can remove /usr/local/lib64 because macOS doesn't use that

  • you will want to decide it you are going to use a package manager, such as homebrew because Apple doesn't provide one. Associated with that, you may want to use pkgconfig from homebrew to automate your compiler and linker flags and switches.

You may also find this helpful, and this.


I modified the Makefile included in the egg package to look like this and it compiles and builds fine on my Mac under macOS Mojave:

CC    = gcc
CFLAGS = -O2 -Wall
DESTDIR =
USERCC   = gcc
USERCCFLAGS = -O2 -Wall
USERFC   = gfortran
USERFCFLAGS = -O2 -Wall

IINC  = -I/opt/X11/include
LLIB  = -L /opt/X11/lib

PREFIX = /usr/local

INCDIR = $(PREFIX)/include
LIBDIR = $(PREFIX)/lib
BINDIR = $(PREFIX)/bin

LOCALINC = -I$(INCDIR)
LOCALLIB = -L$(LIBDIR)

LLINKS = -leggx -lX11 -lm

DEFS = $(VDEFS) $(TDEFS) $(UDEFS) $(PDEFS) $(XDEFS)

all:: _xslave_ mkexheader exec_xslave.h lib egg

install:: lib egg
    if [ ! -d $(DESTDIR)$(LIBDIR) ];then sh install-sh -d $(DESTDIR)$(LIBDIR); fi
    if [ ! -d $(DESTDIR)$(INCDIR) ];then sh install-sh -d $(DESTDIR)$(INCDIR); fi
    if [ ! -d $(DESTDIR)$(BINDIR) ];then sh install-sh -d $(DESTDIR)$(BINDIR); fi
    sh install-sh -m 644 libeggx.a $(DESTDIR)$(LIBDIR)
    sh install-sh -m 644 eggx*.h $(DESTDIR)$(INCDIR)
    sh install-sh -m 755 egg $(DESTDIR)$(BINDIR)
    ranlib $(DESTDIR)$(LIBDIR)/libeggx.a

.c.o:   ; $(CC) $(CFLAGS) $(IINC) $(DEFS) -c $*.c

_xslave_: _xslave_.o _eggx_scrollbar.o
    $(CC) $(CFLAGS) _xslave_.o _eggx_scrollbar.o -o _xslave_ $(LLIB) -lX11

mkexheader: mkexheader.o
    $(CC) $(CFLAGS) mkexheader.o -o mkexheader

exec_xslave.h: mkexheader _xslave_
    ./mkexheader _xslave_ > exec_xslave.h

eggx_base.o: exec_xslave.h

lib:: eggx_base.o eggx_color.o
    ar cruv libeggx.a eggx_base.o eggx_color.o
    ranlib libeggx.a

egg:: egg.sh
    cat egg.sh > egg
    rm -f egg.t
    for i in USERCC "x@@$(USERCC)" USERFC "x@@$(USERFC)" \
      USERCCFLAGS "x@@$(USERCCFLAGS)" USERFCFLAGS "x@@$(USERFCFLAGS)" \
      IINC "x@@$(IINC)" LLIB "x@@$(LLIB)" LOCALINC "x@@$(LOCALINC)" \
      LOCALLIB "x@@$(LOCALLIB)" LLINKS "x@@$(LLINKS)" ; do { \
      if [ -f egg.t ] ; then \
        if [ "$$i" = "x@@" ] ; then \
          cat egg.t | sed -e "s|@@@TARGET@@@||" > egg ; \
        else \
          cat egg.t | sed -e "s|@@@TARGET@@@|$$i|" | sed -e 's|x@@||' > egg ; \
        fi ; \
        rm -f egg.t ; \
      else \
        cat egg | sed -e "s|@$$i@|@@@TARGET@@@|" > egg.t ; \
      fi ; \
    } done
    chmod 755 egg

clean::
    rm -f *.o *.exe egg egg.t libeggx.a _xslave_ mkexheader exec_xslave.h

If you saved the tarball of eggx in directory HOME/eggx, you would need to do this:

cd $HOME/eggx
tar -xvf eggx-0.93r5.tar
cd eggx-0.93r5

Then copy the modified Makefile from above and save it in that directory as Makefile. Now just run:

make

and it should all build fine.



来源:https://stackoverflow.com/questions/59392877/how-to-change-the-following-makefile-so-that-it-works-for-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!