c - Can #ifndef ignore method or variable duplications? -


consider code.

#ifndef foo_h #define foo_h //code #endif 

code can following cases

// case 1:  #define foo 0 
// case 2: void foo_method(){}; 
// case 3: int foo; 

foo.h included in many c files. when compile case 1 without errors, other cases throw errors of duplication.

why when foo.h not concatenated c files except 1 while compiling?

about case 2:
should declare function signature, not body. has nothing pre-processor commands.

in header file (decleration only)

#if <condition> void foo(); #endif 

in c file

#if <condition> void foo(){    //body }  #endif 

about case 3:
it's similar case 2, addition should declare variables in header file if extern , there no need declare them in header file otherwise. if are declared extern, need declared in c file without extern keyword:

in header file:

#if <condition> extern int bar; #endif 

in c file:

#if <condition> int bar; #endif 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -