How preprocessor at c works?

Dear all,
I am trying to understand how c preprocessor works in c

I have built that simple environment


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>


int main(int argc, char *argv])
{
	int i;
#ifdef IPV6
    if( ( i = socket( AF_INET6, SOCK_STREAM, 0 ) ) < 0 )
#else
    if( ( i = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
#endif
    perror("error");
    else{
    	printf("socket created %d");

    }

    return 0;
}

and I am trying to see
a) How to set manually IPV6
b) How to make the system set it.

I have heard about a tool that is called autoconf that can do that by it asks for a template for doing that.

Could you please help me with that?

B.R
Alex

Not sure what you are trying to achieve. But you shouldn’t add any AF_INET vs AF_INET6 logic. Do as explained in Userlevel IPv6 Programming Introduction and let getaddrinfo() decide.

On Tue, 01 May 2012 15:06:03 +0000, alaios wrote:

> Could you please help me with that?

man gcc

Look for “preprocessor directives”

That should get you pointed in the right direction.

(I’m being intentionally cryptic as this /sounds/ like it might be a
homework assignment - if it is, then you need to do the homework yourself
rather than have someone spoon-feed you the answer)

Jim

Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

Thanks a lot on that… I was trying to thing of a small example to learn about those. I have finished already beej’s network programming guide.

I changed slightly my code to

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>


int main(int argc, char *argv])
{
                int i;
#ifdef IPV6
                    if( ( i = socket( AF_INET6, SOCK_STREAM, 0 ) ) < 0 )
#else
                                if( ( i = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
#endif
                                            perror("error");
                        else{
                                        printf("socket created %d
");
#ifdef IPV6
                                        printf("IPV6
");
#else
                                        printf("IPV4
");
#endif

                                            }

                        return 0;
}

and then I can compile it with

gcc testIP.c -D IPV6=NULL -o testIP
username@host~/testIP> ./testIP
socket created 1
IPV6

gcc testIP.c -o testIP
username@host:~/testIP> ./testIP
socket created 1
IPV4

a) If I understand it right the ifdef are “flags”, that flag can not have a value like 0,1,2 but just to be set or unset
b) Then I tried to see how one can automate that process, or in other words is there any way to ask the system to check that there is support for IPv6 and if yes how?

I thus tried the autoconf which generated the file b

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([testIP.c])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h stdlib.h string.h sys/socket.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.
AC_CHECK_FUNCS([socket])

AC_OUTPUT

which seems that is trying to find out if the function socket is implemented in the system… I will try to think a bit more about that.

Regards
Alex