-2

I have a Makefile on my Linux Pc.I want to build and run the same program on MacOS too . How to edit the following Makefile ?

I want to compile a C program that has a header file of a graphics library EGGXProCALL JAXA "eggx.h". I already installed Xcode ,XQuartz.

I have installed EGGX file on the following directory .

[Home@/opt/eggx]$

I have a C file in the following directory.

[Home@~/Desktop/development]$

I have checked the followings .

① gcc works fine here [Home@~/Desktop/development]$

② even the sample program to display a digital clock works , when the current directory is [Home@/opt/eggx]$

PROBLEM

when I tried to build [make] the program from [Home@~/Desktop/development]$ by using the makefile that I had in Linux.

I always get the following error message.

[Home~/Desktop/development]$ sudo make
gcc -c main.c
main.c:7:10: fatal error: 'eggx.h' file not found
#include <eggx.h>
         ^~~~~~~~
1 error generated.
make: *** [main.o] Error 1

[Home@~/Desktop/development]$ 

Here is the 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 
  • 4
    What makes this question different from [your previous question](https://stackoverflow.com/questions/59392877/how-to-change-the-following-makefile-so-that-it-works-for-macos)? Don't post the same question multiple times. – Some programmer dude Dec 20 '19 at 10:26
  • 2
    what is the problem when try to build? compilation, linking, execution? – Mathieu Dec 20 '19 at 10:37
  • Take the compiler in a variable (clang on Mac OS X, but gcc is linked to the same, so should work in most of the cases). Also, you'll need to edit paths whenever possible, so better to take them in paths. Read https://ftp.gnu.org/old-gnu/Manuals/make-3.79.1/html_chapter/make_7.html for more details on using conditional sections in makefiles. – Siddharth Dec 20 '19 at 10:52
  • The problem is that the Makefile does not recognize the header file "eggx.h" – Roman Kshetri Dec 20 '19 at 11:15
  • 1
    Does this answer your question? [How to change the following Makefile so that it works for MacOS?](https://stackoverflow.com/questions/59392877/how-to-change-the-following-makefile-so-that-it-works-for-macos) – mlidal Dec 20 '19 at 11:24
  • @RomanKshetri Post the error exact message, did you installed EGGXProCALL library, if so how? – Mathieu Dec 20 '19 at 11:44
  • @milidal He helped me a lot, but at the eggx library still does not run on my MAC. gcc works fine though. – Roman Kshetri Dec 20 '19 at 13:19
  • @Mathieu ,Yes I installed EGGXProCALL library from the following website. https://www.ir.isas.jaxa.jp/~cyamauch/eggx_procall/index.ja.html I downloaded the eggx-0.93r5.tar.gz and installed it so many times in many directories. – Roman Kshetri Dec 20 '19 at 13:21
  • @Mathieu, The error is " fatal error: eggx.h file not found'. – Roman Kshetri Dec 20 '19 at 13:23
  • Could you edit you question with the exact error message. And add the path to tho file eggx.h. I think you're just missing a `-I` parameter in compiler invocation But without clear error message, I'm only guessing – Mathieu Dec 20 '19 at 13:24
  • @Mathieu sure.Thank you.I will edit the question and post the exact error . – Roman Kshetri Dec 20 '19 at 13:35
  • @Mathieu ,I edited the question in detail. I hope you can help me from here.it's been some days I am struggling with this issue. – Roman Kshetri Dec 20 '19 at 14:13
  • @Siddharth I edited my problem in details. – Roman Kshetri Dec 20 '19 at 14:29
  • @Someprogrammerdude I have updated my problem in details – Roman Kshetri Dec 20 '19 at 14:29
  • Well you can try to find the `eggx.h` file with find: `find / -name "eggx.h" 2> /dev/null` – Mathieu Dec 20 '19 at 14:51
  • I found its here. /usr/local/eggx-0.93r5/eggx.h – Roman Kshetri Dec 20 '19 at 14:55
  • 1
    That means you must use `gcc -I/usr/local/eggx-0.93r5 ...` – Mark Setchell Dec 20 '19 at 15:00
  • Now find the library you have built `find /usr -name libeggx.a` – Mark Setchell Dec 20 '19 at 15:05
  • for example ,if my c file is main.c ,then..,how should I compile it? – Roman Kshetri Dec 20 '19 at 15:06
  • Like @MarkSetchell suggested, you need to provide the include path to eggx.h file while compiling... – Siddharth Dec 20 '19 at 15:06
  • Please find the library first – Mark Setchell Dec 20 '19 at 15:06
  • @MarkSetchell I found that the library is here /usr/local/eggx-0.93r5/libeggx.a – Roman Kshetri Dec 20 '19 at 15:07
  • 1
    So that means you need to use `gcc -I/usr/local/eggx-0.93r5 -L/usr/local/eggx-0.93r5 -l eggx main.c -o program` along with the other flags for X11 I gave you in the other question. – Mark Setchell Dec 20 '19 at 15:08
  • For X11, add in `-I /opt/X11/include` and `-L /opt/X11/lib -lx11` – Mark Setchell Dec 20 '19 at 15:11
  • I edited the makefile you provided me last day like the following. **IINC = -I/opt/X11/include** **LLIB = -L /opt/X11/lib -lx11** The following error is generated again. make: No rule to make target `_xslave_.o', needed by `_xslave_'. Stop. – Roman Kshetri Dec 20 '19 at 15:25

2 Answers2

2

You must do things in the correct order and not proceed to the next step until you have correctly completed the previous step.

1. Download, and extract the library.

The download is normally done with git clone or scp to copy the source files from somewhere.

The extract (unpack from archive) is normally done with:

tar -xvf eggx-0.93r5.tar

That will normally create a new directory (with the same name as the tar-file but without the .tar extension) like:

eggx-0.93r5

2. Build the library.

Normally you need to change directory into the newly created one and run make. I gave you the Makefile last time so you need to do:

cd eggx-0.93r5
cp MAKEFILEFROMMARK Makefile
make

There should be no errors. If there are errors, you must solve them and then run:

make clean        # delete any rubbish from previous failed build
make

3. Install the library.

You normally do this with:

make install

What that actually does depends on the package you are installing, but as a general rule, it will copy the header files and the libraries you just made into a "known" location, like /usr/local or /opt/package. The idea is to make all the files your own code will need available to all users of the computer by "publishing" or installing them to known locations.

4. Work out how to compile a simple C program that uses the library.

You should do the following steps in a completely different directory from where you downloaded the library to - do not mix your code with the library's code.

If your program uses eggx.h like this:

#include "eggx.h"

then you need to find where eggx.h is like this:

find /usr /opt /Users -name eggx.h

If that results in:

/path/to/somewhere/include/eggx.h

that means you must add this to your gcc command to tell the compiler how to find it:

gcc -I/path/to/somewhere/include ...

If your library is called libeggx.a, you need to find that too:

find /usr /opt /Users -name "libegg*a"

If that results in:

/path/to/somewhere/lib/libeggx.a

that means you need to add this to your gcc command to tell the linker where it is and what it is called:

gcc ... -L/path/to/somewhere/lib -leggx

If your program uses X11, you must install XQuartz on a Mac, and add the flags/switches for X11 into your compilation:

gcc ... -I /opt/X11/include -L /opt/X11/lib -lx11 ...

So, putting all that together, if your program is called program.c, you will compile and link with:

gcc program.c -o program -I/path/to/somewhere/include -I /opt/X11/include -L /opt/X11/lib -lx11 -L/path/to/somewhere/lib -leggx

and then run with:

./program

5. Make a Makefile that enshrines what you learned at (4).

That might look something like this:

EGGINC = -I /path/to/somewhere/include
EGGLIB = -L /path/to/somewhere/lib -leggx

X11INC = -I /opt/X11/include
X11LIB = -L /opt/X11/lib -lx11

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

$(OBJS).o: $(OBJS).c
    gcc -I/usr/local/include $(EGGINC) $(X11INC) -c $(OBJS).c
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
0

You build your program in two steps:

  1. Build the source file into an object file
  2. Link the object file with libraries to create the final executable program

The preprocessor (which handles #include directives) is part of the building of the object file. So all flags that are needed for creation of the object files (like the -I option) should be present there and only there.

So the two rules could be changed as follows:

$(OBJS): $(OBJS).o
    gcc $(OBJS).c -o $(OBJS) -L/usr/local/lib64 -leggx -lX11 -lm

$(OBJS).o: $(OBJS).c
    gcc -I/usr/local/include -c $(OBJS).c

Of course that assumes that the EGGX library was installed in /usr/local.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621