Professional Web Applications Themes

Makefile in Unix - UNIX Programming

Hi, I have a project in C and I want to compile it under Unix system through a Makefile. I am learning Makefile and I think I miss something. Unix os run on AIX machine, and I use the xlc compiler and the ld linker. my Makefile is as follow (there is something wrong in the linker command...), the "main" routine is in DOSMAIN.c file, all files include the same .h file which is then looking for some basic libraries (stdio.h, math.h, string.h, ...) CC = xlc LD = ld CFLAGS = -c LDFLAGS = OBJS = DOSMAIN.o AERODAT.o ... ...

  1. #1

    Default Makefile in Unix

    Hi,

    I have a project in C and I want to compile it under Unix system through a
    Makefile. I am learning Makefile and I think I miss something. Unix os run
    on AIX machine, and I use the xlc compiler and the ld linker.

    my Makefile is as follow (there is something wrong in the linker
    command...), the "main" routine is in DOSMAIN.c file, all files include the
    same .h file which is then looking for some basic libraries (stdio.h,
    math.h, string.h, ...)

    CC = xlc
    LD = ld
    CFLAGS = -c
    LDFLAGS =
    OBJS = DOSMAIN.o AERODAT.o ... and so many (about 40 files)
    SRCS = AERODAT.c ALOC.c ... as many as .o definition, I do not state any
    ..h files, since it is stated as "#include ..." in each .c file

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

    build: $(OBJS)
    $(LD) $(LDFLAGS) $(OBJS) -o project.exe


    when "make" is called, all the .o are created successfully, but the linker
    gives 2 errors:
    1st error (warning): Entry point not found: __start
    2nd error: No csects or exported symbols have been saved
    and a line saying "the error code from the last command is 8"

    what is wrong?

    Thanks

    Mat



    Mathieu Guest

  2. #2

    Default Re: Makefile in Unix

    "Mathieu Fregeau" <ca> writes: 
     
     


    Typically, in most Unix systems, its the C compiler driver $CC (in
    your case xlc) that knows how to properly link a binary to execute it
    for you as well as knows how to compile a file. You almost never call
    ld directly yourself (you really don't call the C compiler directly
    your self either, you are calling a program that knows how to call it
    properly).

    So, your build line should be $CC $(OBJS) -o project

    No .exe files in unix, its not very cool looking.





    --
    Doug McIntyre com
    Network Engineer/Jack of All Trades
    Vector Internet Services, Inc.
    Doug Guest

  3. #3

    Default Re: Makefile in Unix

    Thank you Doug,

    I think there might be something wrong with my system configuration. The
    build line is now:

    build:
    $(CC) $(OBJS) -o project

    but the system "freezes" when I call "make build"

    the same when I call my "clean" target which is like this:

    clean:
    rm -f *.o

    note that there is no space or tab right after the colon (:) and there is a
    tab on the line right after the colon.

    ....? any idea?

    Thanks

    Mat

    "Doug McIntyre" <com> wrote in message
    news:3f9ae555$0$41287$visi.com... [/ref]
    a [/ref]
    run 

    > [/ref]
    linker 
    >
    >
    > Typically, in most Unix systems, its the C compiler driver $CC (in
    > your case xlc) that knows how to properly link a binary to execute it
    > for you as well as knows how to compile a file. You almost never call
    > ld directly yourself (you really don't call the C compiler directly
    > your self either, you are calling a program that knows how to call it
    > properly).
    >
    > So, your build line should be $CC $(OBJS) -o project
    >
    > No .exe files in unix, its not very cool looking.
    >
    >
    >
    >
    >
    > --
    > Doug McIntyre com
    > Network Engineer/Jack of All Trades
    > Vector Internet Services, Inc.[/ref]


    Mathieu Guest

  4. #4

    Default Re: Makefile in Unix

    "Mathieu Fregeau" <ca> wrote: 

    Don't put the '-c' option in the CFLAGS variable.
     

    Hard wire the -c option into this rule:

    .o.c:
    $(CC) $(CFLAGS) $(CPPFLAGS) $< -c -o $

    However, that rule is probably built into your make, and doesn't
    need to be specified. See what happens if you take it out.
    Note that $< is the variable that holds the prerequisite, and
    $ is the variable that holds the target. In this case $< is
    the same as the $.c that you are using.

    You can now add things to the definition of CFLAGS to optimize,
    to increase the level of warnings, or whatever other options you
    might like to use.
     

    build: $(OBJS)
    $(CC) $(LDFLAGS) $(OBJS) -o project

    Or you can continue to use $(LD), but then you want to define it
    as

    LD = xlc

    or

    LD = $(CC)

    As others have indicated, when you call cc to link the object files,
    it actually does a lot more than just link the object files that you
    know about. It also links in the start module, some version of libc,
    and maybe a lot of other things depending on the platform. You can
    probably read the man page for cc and find an option that will cause
    verbose reporting, and that will tell you what it is actually doing.
    Be prepared for a whole page of information.

    Here is something else you want to add to your Makefile. For
    all headers that are include by all source files, do

    $(OBJS): project.h headers.h

    For headers that are included by only some source files, do

    DOSMAIN.o: dosmain.h
    ALOC.o: aloc.h header1.h

    And so on. There can be one or several header files listed per
    object file. The result will be that a change to either
    project.h or headers.h would cause all of the $OBJS files to be
    rebuilt, while a change to dosmain.h would cause only DOSMAIN.o
    to be rebuilt.
     

    --
    Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
    Ukpeagvik (Barrow, Alaska) com
    Floyd Guest

  5. #5

    Default Re: Makefile in Unix

    # build:
    # $(CC) $(OBJS) -o project
    ^^^^

    Are these spaces or a tab? Rules in this syntax should be proceeded
    with a tab, not spaces.

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    Mention something out of a Charleton Heston movie, and suddenly
    everybody's a theology scholar.
    Derk Guest

Similar Threads

  1. Replies: 3
    Last Post: April 18th, 06:24 PM
  2. Replies: 2
    Last Post: August 14th, 12:58 PM
  3. Replies: 1
    Last Post: July 22nd, 03:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139