0

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.

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.

This is what is shown in the terminal:

> Executing task in folder Led-Pot_Circuit_Test: .\Build.cmd 16f887 <


C:\Users\raymu_000\Documents\Programming\VisualStudioCode\PIC_C++_on_VSCode\Led-Pot_Circuit_Test>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>C:\Progra~1\Microchip\xc8\v2.32\bin\xc8.exe --chip=16f887 --outdir=".\output-files" ".\main.c"
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>C:\Progra~1\Microchip\xc8\v2.32\bin\xc8.exe --chip=16f887 --outdir=".\output-files" ".\Project\InitApp.c"
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 "_main" not defined
(908) exit status = 1
(908) exit status = 1
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command .\Build.cmd 16f887" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

My main class:

//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 <xc.h>
#include <pic16f887.h>
#include "Project\user.h"


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);
    }
}

This is my attempt at making a library so that I can "easily" transfer some useful code later onto another project:

//user.h

#ifndef USER_H
#define USER_H

void init_adc(void);
unsigned short adc_convert(unsigned char);

#endif

Useful code:

//InitApp.c

#include <xc.h>
#include <pic16f887.h>
#include "user.h"

/*
 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<<8)|ADRESL; // return the result
}

Pic for reference: enter image description here

Here are the contents of the "output-files" Folder, when I run the "Build.cmd" task all the files from the compiler go here, but I am missing a few (like ".hex" file) since the program cannot compile properly. enter image description here

Do I need to have a main class inside of InitApp.c class? How does one properly compile and link C files together??

I would appreciate amy help and advice, thanks.

5
  • 1
    don't use backslash in include paths, what is the content of your ./output-files directory, where is your link command Commented Mar 1, 2021 at 5:32
  • 1
    What is the method to get a single backslash in a literal part (backticks)? Commented Mar 1, 2021 at 5:36
  • @rioV8 Is not using backslash on include paths just a general rule?? I just added an image with the content of the output-files folder. I don't believe I have link commands, in fact, I'm not even sure I know what a link command is. I'm trying to look up information about that now, where should my link commands be?? Commented Mar 1, 2021 at 18:26
  • 1
    the compiler generates .obj files, the Linker combines .obj files and libraries to an executable. Search the doc of your compiler on the complete build workflow Commented Mar 1, 2021 at 20:01
  • @rioV8 Wow thanks! Thank you for pointing me on the right path, I think I've figured it out. By reading the XC8 Compiler User's Guide I learned that I first needed to compile the C class that I want to use as a library into an ".lpp" file and then compile that together with my "main.c". I will try to write a proper response to my own question later. Commented Mar 1, 2021 at 21:19

1 Answer 1

1

I know it's too late, but someone else could be thankful.

You have to compile all archives, one by one, and then link them all. The correct form it would be something like this:

%XC_DIR%\bin\xc8.exe --chip=%1 --outdir=".\output-files" ".\Project\InitApp.c"
%XC_DIR%\bin\xc8.exe --chip=%1 --outdir=".\output-files" ".\main.c"

until here you are ok. Every command generates a p1 file (main.p1, InitApp.p1), now you have to link them all.

%XC_DIR%\bin\xc8.exe --chip=%1 --outdir=".\output-files" ".\output-files\InitApp.p1" ".\output-files\main.p1"

That may resolve your issue. Consider first compiling files that doesn't depends on any file, and the last one to compile the main.c that includes all other files.

Hope this is going to help someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.