LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   ld : warnings and errors (http://www.linuxquestions.org/questions/programming-9/ld-warnings-and-errors-521648/)

rajesh_b 01-22-2007 07:21 AM

ld : warnings and errors
 
Hi,

I have a simple .c file and a make file to compile it. But when i run make i am getting some warnings and errors.

My source file

#include <stdio.h>

int main()
{
int a = 0;
printf("%d\n",a);
return 0;
}


Makefile:
SRC= temp.c
OBJ= temp.o

CFLAGS = -c

all: Check

Check: $(SRC)
gcc $(CFLAGS) $(SRC)
ld -o check $(OBJ)
clean:
-rm -rf *o


I am getting following warnings and errors:
ld: warning: cannot find entry symbol _start; defaulting to 08048074
temp.o(.text+0x23): In function `main':
: undefined reference to `printf'
make: *** [dal] Error 1

Can any body tell me what is the problem?


Regards,
Karim

KenJennings 01-22-2007 07:57 AM

Quote:

Originally Posted by rajesh_b
[...]
I am getting following warnings and errors:
ld: warning: cannot find entry symbol _start; defaulting to 08048074
temp.o(.text+0x23): In function `main':
: undefined reference to `printf'
make: *** [dal] Error 1

Are you building this on unix? linux? what?

Is there a specific reason you need to use ld directly? gcc can invoke linking itself and quietly include necessary system object files. " gcc -o check temp.o " should work.

Man ld includes a simple example of invoking ld on a simple program (in a specific unix environment.):

Code:

The  linker  supports a plethora of command-line options, but in actual practice
few of them are used in any particular context.  For instance, a frequent use of ld is
to link standard Unix object files on a standard, supported  Unix  system.  On such a
system, to link a file "hello.o":

              ld -o <output> /lib/crt0.o hello.o -lc


matthewg42 01-22-2007 08:50 AM

Please use [code] tags to improve reabaility of your posts.

Try a different Makefile. This one is quite nice and easy to maintain for small projects:
Code:

CC = gcc
CFLAGS = -g
LFLAGS =

OBJECTS = temp.o
EXECUTABLE = myprog

$(EXECUTABLE) : $(OBJECTS)
        gcc $(LFLAGS) $(OBJECTS) -o $@

clean :
        rm -f $(OBJECTS) $(EXECUTABLE)

%.o : %.c
        gcc -c $(CFLAGS) $< -o $@

As you add more sources, just add the names of the object files they will build to OBJECTS. he rest is automatic.

To prevent warnings, and to be standards compliant, don't forget to add the proper arguments to main():
Code:

#include <stdio.h>

int main(int argc, char** argv)
{
        int a = 0;
        printf("The number is %d\n",a);
        return 0;
}



All times are GMT -5. The time now is 12:46 AM.