Search code examples
cprecompiled-headers

ld: symbol(s) not found for architecture x86_64 when including header file


I am writing the following code to print the underlying Operative System. It's incomplete and might be wrong (appreciate advices for the wrong, no need for complete as it's for learning purposes).

platform.c

#include <stdio.h>                                                              
                                                                                
void platform(int a)                                                            
{                                                                               
    #if __APPLE__                                                               
        printf("recognized apple\n");                                                                                           
    #elif __LINUX__                                                             
        printf("linux\n");                                                      
    #else                                                                       
        printf("unknown\n");                                                    
    #endif                                                                                                                                
                                                                                
    printf("%i\n",a);                                                           
} 

If I add main() to platform.c and compile it, it works fine. However, as I am experimenting for learning purposes, including a header file:

platform.h

#ifndef PLATFORM_H_   /* Include guard */                                       
#define PLATFORM_H_                                                             
                                                                                
void platform(int);                                                             
                                                                                
#endif // PLATFORM_H_  

                  

and lastly including the header in a new file:

Try.c

#include "./platform.h"                                                         
                                                                                
                                                                                
int main()                                                                      
{                                                                               
    platform(4);                                                                
}                                      

I compile it as: gcc try.c and I get:

Undefined symbols for architecture x86_64:
  "_platform", referenced from:
      _main in lala-10eeb5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
                   

Please, go easy on me and thank you for your help!


Solution

  • You have to link platform.c to Try.c Try using this command:

    gcc try.c platform.c
    

    You only included the declaration, not the implementation. This leads to a successful compiling, but linking will fail, as there is no implementation for platform.