Header (.h) and Source (.c) files

Moderators: TomKerekes, dynomotion

Post Reply
gui_marchioro
Posts: 59
Joined: Sun Aug 21, 2022 11:22 pm

Header (.h) and Source (.c) files

Post by gui_marchioro » Mon Nov 07, 2022 12:17 pm

Hello,

I would like to organize my project files using .h for function prototypes and variable declarations, and .c file to the variable definitions and the "implementation" ("executable code" to be compiled).

I have implemented this, following what is described in this link https://riptutorial.com/c/example/3250/ ... her-c-file.

However, by trying such organization I am getting a compilation error.
Has anyone made something in this way and could make it work?

Sincerely,
Guilherme

gui_marchioro
Posts: 59
Joined: Sun Aug 21, 2022 11:22 pm

Re: Header (.h) and Source (.c) files

Post by gui_marchioro » Mon Nov 07, 2022 12:27 pm

Here is an example of what I have implemented:

Program to be executed when the init button is pressed:
InitConfigButtonAction.c

Code: Select all

#include "InitConfig.h"
#include "KMotionDef.h"

// Initialization program that runs when the Init button is pressed inside KMotionCNC
main()
{
    // Initializes Konnect boards
    IOboardsConfig();
    // Initializes KFlop and Kanalog board
    InitConfig();
}
Header file with defines and function declarations:
InitConfig.h

Code: Select all

#ifndef InitConfig_h
#define InitConfig_h

#define ENABLE_ALL_AXIS_PIN 1056 // Brake enabled
#define INIT_EXECUTED_VAR 180

// Initial configuration for Axis and IOBoards
void InitConfig();

// Configure the Konnect IO boards connected with the KFLOP main board
void IOboardsConfig();

#endif /* InitConfig_h */
Source file with function implementation:

Code: Select all

#include "InitConfig.h"
#include "KMotionDef.h"

// Initial configuration for Axis and IOBoards
void InitConfig()
{
	// Supressed code for example purposes
}

// Configure the Konnect IO boards connected with the KFLOP main board
void IOboardsConfig()
{
    InitAux();
	AddKonnect(0,&VirtualBits,VirtualBitsEx);
	AddKonnect(1,VirtualBitsEx+1,VirtualBitsEx+2);
}
When I try to execute the Init button I get the following error:
header-file-error.png

User avatar
TomKerekes
Posts: 2540
Joined: Mon Dec 04, 2017 1:49 am

Re: Header (.h) and Source (.c) files

Post by TomKerekes » Mon Nov 07, 2022 5:39 pm

Hi Guilherme,

The example in your reference compiles 2 files separately and then links the 2 files together.

KMotion is set up to simply compile one file and link it. So you must also include InitConfig.c in InitConfigButtonAction.c to form one .c file with everything.

There are ways to have multiple files linked together using the TI Compiler but it is somewhat complicated using batch files that you must create.
Regards,

Tom Kerekes
Dynomotion, Inc.

gui_marchioro
Posts: 59
Joined: Sun Aug 21, 2022 11:22 pm

Re: Header (.h) and Source (.c) files

Post by gui_marchioro » Mon Dec 05, 2022 11:07 am

Hello Tom,

Alright, could you give me some examples of how to do this using batch files?

Sincerely,
Guilherme

User avatar
TomKerekes
Posts: 2540
Joined: Mon Dec 04, 2017 1:49 am

Re: Header (.h) and Source (.c) files

Post by TomKerekes » Mon Dec 05, 2022 9:18 pm

Hi Guilherme,

See the folder:
C:\KMotion435h\C Programs\TI_Compiler

If you aren't familiar with invoking the Compiler, Linker, and Linker command files then just include the C file as explained earlier.
Regards,

Tom Kerekes
Dynomotion, Inc.

gui_marchioro
Posts: 59
Joined: Sun Aug 21, 2022 11:22 pm

Re: Header (.h) and Source (.c) files

Post by gui_marchioro » Thu Dec 08, 2022 3:50 pm

Hello,

I am not familiar with the TI Compiler, but I am with compiling and linking operations like in the example from my first post, which can be found here: https://riptutorial.com/c/example/3250/ ... her-c-file

I really would like to apply this development pattern in my project. So, I would be very grateful if you give me a simple example of using the TI compiler to compile .c files and then link them together to form a .out file.

I have already downloaded the compiler software from Texas Instruments and tested it with the example you mentioned in the previous post.

Sincerely,
Guilherme

User avatar
TomKerekes
Posts: 2540
Joined: Mon Dec 04, 2017 1:49 am

Re: Header (.h) and Source (.c) files

Post by TomKerekes » Sat Dec 10, 2022 5:49 pm

Hi Guilherme,

Here is an example batch file MakeMainFile1File2Thread2.bat that will compile MainFile1.c and File2.c and link them to make a MainFile1(2).out to execute in Thread2. Copy files to the C:\KMotion435h\C Programs\TI_Compiler folder. Remove any .txt extensions.
Attachments
lnkThread2.cmd.txt
(966 Bytes) Downloaded 15 times
File2.c
(115 Bytes) Downloaded 15 times
File2.h
(24 Bytes) Downloaded 14 times
MainFile1.c
(114 Bytes) Downloaded 14 times
LinkMainFile1File2.lkf.txt
(95 Bytes) Downloaded 17 times
MakeMainFile1File2Thread2.bat.txt
(346 Bytes) Downloaded 18 times
Regards,

Tom Kerekes
Dynomotion, Inc.

Post Reply