title: MPLAB X XC8 Error: 0: (499) undefined symbol (VS Code)
body: <p>I am new to coding in C and I cannot figure out the proper way to link my files together so that I can use code from one class in another. I'm working on a project which will likely become a very long program so I am trying to avoid cluttering my main class with a bunch of functions. Instead, I would like to keep them separate and make it easy to transfer the code into any future project.</p>
  <p>From looking up similar issues I am assuming this is somewhat simple and that I am probably struggling so much with it because I'm missing the basics of coding in C.</p>
  <p>This is what is shown in the terminal:</p>
  <pre><code>&gt; Executing task in folder Led-Pot_Circuit_Test: .\Build.cmd 16f887 &lt;
  
  
  C:\Users\raymu_000\Documents\Programming\VisualStudioCode\PIC_C++_on_VSCode\Led-Pot_Circuit_Test&gt;mkdir output-files
  A subdirectory or file output-files already exists.
  
  C:\Users\raymu_000\Documents\Programming\VisualStudioCode\PIC_C++_on_VSCode\Led-Pot_Circuit_Test&gt;C:\Progra~1\Microchip\xc8\v2.32\bin\xc8.exe --chip=16f887 --outdir=&quot;.\output-files&quot; &quot;.\main.c&quot;
  C:\Progra~1\Microchip\xc8\v2.32\pic\bin\picc --chip=16f887 --outdir=.\output-files .\main.c
  Microchip MPLAB XC8 C Compiler V2.32
  Build date: Feb  1 2021
  Part Support Version: 2.32
  Copyright (C) 2021 Microchip Technology Inc.
  License type: Node Configuration
  
  0: (499) undefined symbol:
          _adc_convert(output-files\main.obj)
  (908) exit status = 1
  (908) exit status = 1
  
  C:\Users\raymu_000\Documents\Programming\VisualStudioCode\PIC_C++_on_VSCode\Led-Pot_Circuit_Test&gt;C:\Progra~1\Microchip\xc8\v2.32\bin\xc8.exe --chip=16f887 --outdir=&quot;.\output-files&quot; &quot;.\Project\InitApp.c&quot;
  C:\Progra~1\Microchip\xc8\v2.32\pic\bin\picc --chip=16f887 --outdir=.\output-files .\Project\InitApp.c
  Microchip MPLAB XC8 C Compiler V2.32
  Build date: Feb  1 2021
  Part Support Version: 2.32
  Copyright (C) 2021 Microchip Technology Inc.
  License type: Node Configuration
  
  Non line specific message: (1091) main function &quot;_main&quot; not defined
  (908) exit status = 1
  (908) exit status = 1
  The terminal process &quot;C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command .\Build.cmd 16f887&quot; terminated with exit code: 1.
  
  Terminal will be reused by tasks, press any key to close it.
  </code></pre>
  <p>My main class:</p>
  <pre><code>//main.c
  
  // CONFIG1
  #pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
  #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  #pragma config MCLRE = OFF      // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
  #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
  #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
  #pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
  #pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
  #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
  #pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
  
  // CONFIG2
  #pragma config BOR4V = BOR21V   // Brown-out Reset Selection bit (Brown-out Reset set to 2.1V)
  #pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
  
  #define _XTAL_FREQ 8000000 // 8Mhz
  
  // #pragma config statements should precede project file includes.
  // Use project enums instead of #define for ON and OFF.
  
  #include &lt;xc.h&gt;
  #include &lt;pic16f887.h&gt;
  #include &quot;Project\user.h&quot;
  
  
  void main(void) {
  
      //InitApp();
  
      unsigned short adc_value; // variable to hold ADC conversion result in/* Configure the oscillator for the device
  
      ANSELbits.ANS0 = 0; // Disable analog on pin RA0
      TRISAbits.TRISA0 = 0; // Set pin RA0 as output
  
      while(1) {
          PORTAbits.RA0 = 0; // Set RA0 to LOW
          // calculate wait time
          adc_value = adc_convert(1); // perform A/D conversion using input from channel 1 (AN1)
          __delay_ms(500); // Right shift adc_value two spaces to convert to milliseconds
          PORTAbits.RA0 = 1; // Set RA0 to HIGH
          adc_value = adc_convert(1);
          __delay_ms(500);
      }
  }
  </code></pre>
  <p>This is my attempt at making a library so that I can &quot;easily&quot; transfer some useful code later onto another project:</p>
  <pre><code>//user.h
  
  #ifndef USER_H
  #define USER_H
  
  void init_adc(void);
  unsigned short adc_convert(unsigned char);
  
  #endif
  </code></pre>
  <p>Useful code:</p>
  <pre><code>//InitApp.c
  
  #include &lt;xc.h&gt;
  #include &lt;pic16f887.h&gt;
  #include &quot;user.h&quot;
  
  /*
   Initialize the Analog to Digital Converter
   */
  void init_adc(void) {
      TRISAbits.TRISA1 = 0b1; // Set pin RA1 as Input
      //ANCON0bits.ANSEL1   =0b1;
      ADCON1bits.VCFG0 = 0; // set v+ reference to Vdd
      ADCON1bits.VCFG1 = 0; // set v- reference to Vss (Gnd)
      ADCON1bits.ADFM = 1; // right justify the output
      ADCON0bits.ADCS = 0b01; // use Fosc/8 for clock source
      ADCON0bits.ADON = 1; // turn on the ADC
  }
  
  /*
   Preform an analog to digital conversion. 
  @param channel The ADC input channel to use. 
  @return The value of the conversion.
  */
  unsigned short adc_convert(unsigned char channel) {
      ADCON0bits.CHS = channel; // select the given channel
      ADCON0bits.GO = 1; // start the conversion
      while(ADCON0bits.GO_DONE); // wait for the conversion to finish
      return(ADRESH&lt;&lt;8)|ADRESL; // return the result
  }
  </code></pre>
  <p>Pic for reference:
  <a href="https://i.stack.imgur.com/wofXa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wofXa.png" alt="enter image description here" /></a></p>
  <p>Here are the contents of the &quot;output-files&quot; Folder, when I run the &quot;Build.cmd&quot; task all the files from the compiler go here, but I am missing a few (like &quot;.hex&quot; file) since the program cannot compile properly.
  <a href="https://i.stack.imgur.com/xqjvi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xqjvi.png" alt="enter image description here" /></a></p>
  <p>Do I need to have a main class inside of InitApp.c class?
  How does one properly compile and link C files together??</p>
  <p>I would appreciate amy help and advice, thanks.</p>