Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

conditional compilation ?

Hello.

I'm working with two kits of development NIOSII.

I wish to use the following conditional compilation in a common file (as user.h) :

// --- choice of board ----------------------------------

# define NIOSII_1

//#define NIOSII_2

// --- choice of MAC address -------------------------

# ifdef NIOSII_1

# ifndef FLAG_ADRESSE_MAC_ETHERNET

# define FLAG_ADRESSE_MAC_ETHERNET

const char adresse_mac_ethernet[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06} ;

# endif // FLAG_ADRESSE_MAC_ETHERNET

# endif // NIOSII_1

# ifdef NIOSII_2

# ifndef FLAG_ADRESSE_MAC_ETHERNET

# define FLAG_ADRESSE_MAC_ETHERNET

const char adresse_mac_ethernet[6] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16} ;

# endif // FLAG_ADRESSE_MAC_ETHERNET

# endif // NIOSII_2

The result of compilation is :

> obj/file.o *123: multiple definition of `adresse_mac_ethernet' ../file.c:218: first defined here

Does conditional compilation function ? Or do I make an error ?

Best regards

bbo

5 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    maybe a problem that not all files are recompiled (which include your user.h),

    after you changed your# define NIOSII_1.

    so you may have some .o files with old and some .o files with new definition.

    try to recompile all
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you for your answer

    I have just used the command "File / Refresh" to 'refresh' my whole project.

    I have used the command "Project / Rebuild All".

    Unfortunately, there is no change.

    But I have just found this link on the ALTERA' s site :

    http://www.altera.com/support/kdb/2000/01/...32000_6557.html (http://www.altera.com/support/kdb/2000/01/rd01032000_6557.html)

    Could this explain my problem partly ?

    I tried to use the operators# if defined(...),# if !defined(...), ... same result

    Best regards.

    bbo
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    If you try to# include your "user.h" file in more than one module, then you will be attempting to define multiple global variables of the same name, hence the error. If this is the case you may need to create a further condition in only one module. Perhaps something like:

    # define MAIN_MODULE# include "user.h"# undef MAIN_MODULE

    then in your user.h file, something like:

    # ifdef MAIN_MODULE

    const char adresse_mac_ethernet[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06} ;# else

    extern const char adresse_mac_ethernet[];# endif

    Good luck
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    yes that's it

    adresse_mac_ethernet will be defined in all files which include your user.h,

    the flag FLAG_ADRESSE_MAC_ETHERNET does not prevent this.

    it's better to put the definition of variables in .c files and a extern

    declaration for it in the header.

    eg

    file: user.h

    ------------------------------------------------------------------------------

    // --- choice of board ----------------------------------

    # define NIOSII_1

    //#define NIOSII_2

    extern const char adresse_mac_ethernet[6];

    ------------------------------------------------------------------------------

    file: user.c

    ------------------------------------------------------------------------------# ifdef NIOSII_1# warning "******************NIOSII_1 defined"

    const char adresse_mac_ethernet[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06} ;# endif // NIOSII_1

    # ifdef NIOSII_2# warning "************** NIOSII_2 defined"

    const char adresse_mac_ethernet[6] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16} ;# endif // NIOSII_2
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi. Excuse me for the response time.

    I had arrived at the same solution but I was not satisfied.

    I thus have modify the way of writing the address :

    # ifdef NIOSII_1

    // adresse MAC de la carte en hexadecimal# define ADRESSE_MAC "01:02:03:04:05:06"

    # endif

    # ifdef NIOSII_2

    // adresse MAC de la carte en hexadecimal# define ADRESSE_MAC "11:12:13:14:15:16"

    # endif

    and I have to create a function ether_aton(ADRESSE_MAC) :

    void ether_aton(char *pt_addr_ethernet)

    {

    int i ;

    int adresse_tmp[6] ;

    int *pt_int = adresse_tmp ;

    // conversion de la chaine ASCII en adresse MAC 48 bits

    i = sscanf(pt_addr_ethernet, " %02x:%02x:%02x:%02x:%02x:%02x",

    (pt_int + 0), (pt_int + 1), (pt_int + 2),

    (pt_int + 3), (pt_int + 4), (pt_int + 5)) ;

    // mise au format 8 bits des composantes de l'adresse MAC

    for (i = 0 ; i < 6 ; i++)

    {

    adresse_mac_ethernet = (unsigned char)(adresse_tmp) ;

    }

    }

    Thank you for all.