Linux C hello world

1. vi functions.c

#include <stdio.h>

void hello_world(void)
{
    printf("hello world!!\n");
}


2. vi hello.c
#include <stdio.h>
#include <stdlib.h>

void hello_world(void);

int main(int argc, char **argv)
{
    hello_world();
    exit(0);
}


3. Make Executable hello
# Object Files
$ gcc -g -c functions.c hello.c
# Executable hello
$ gcc -g -o hello functions.o hello.o
# Run hello
$ ./hello


4. To make functions shared
# create functions.o
$ gcc -fPIC -c functions.c
# Create functions.so
$ gcc -shared -o libfuc.so functions.o
# Make libfuc.so shared
$ mv libfuc.so /lib/
# Link libfuc.so to output
$ gcc -o hello -lfuc hel.c
# to see the libraries linked
$ ldd hello
$ ./hello

Posted in C, Linux | Tagged , | Leave a comment