开发者

Make a commandline tool from C program

If I want to make a commandline tool using makefile for, say this C program:

# include<stdio.h>

int main()
{
    printf("Hello World!");
    return 0;
}

I have no previous experience with makefile or linux shell commands whatsoever. Just starting out. Can anyone tell me how to开发者_如何学Go go about it?


You don't really need a makefile for a single source file like this - just compile it like this:

$ gcc -Wall foo.c -o foo

If you really want a makefile though then you can do something like this:

#
# makefile
#

foo: foo.c
    gcc -Wall foo.c -o foo

then from the command line you could say:

$ make foo

or even just

$ make


I don't have a Unix box handy, but ISTR that make(1) has some default rules that should allow you to simply type make hello and it will find hello.c and generate a binary named 'hello'. Of course, for anything more complicated, you'll want to read up on make(1) or its descendents, man make is a good place to start.


Makefile:

hello: hello.c
    gcc -Wall -o hello hello.c


$ make hello
$ ./hello

You might benefit from this.

Also, your program is missing a newline. You probably wanted to print "Hello World!\n".


$cat makefile
main:main.c
    gcc -o main main.c

clean:
    rm main
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜