Linux C hello world

1. vi functions.c [codesyntax lang="c"]
#include <stdio.h>

void hello_world(void)
{
    printf("hello world!!\n");
}
[/codesyntax]
2. vi hello.c [codesyntax lang="c"]
#include <stdio.h>
#include <stdlib.h>

void hello_world(void);

int main(int argc, char **argv)
{
    hello_world();
    exit(0);
}
[/codesyntax]
3. Make Executable hello [codesyntax lang="bash"]
# Object Files
$ gcc -g -c functions.c hello.c
# Executable hello
$ gcc -g -o hello functions.o hello.o
# Run hello
$ ./hello
[/codesyntax]
4. To make functions shared [codesyntax lang="bash"]
# 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
[/codesyntax]
Posted in C, Linux| Tagged , |

Comments are closed.