💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Introduction
  • Step 1. Workspace Folder
  • Create local directory for programming
  • Step 2. Create a tutorial C Project
  • Step 3. Create your Header files
  • Step 4. Include your Header files

Was this helpful?

  1. C Programming
  2. C-Programming Lessons

Creating Header Lib

Introduction

You will learn how to create and maintain my own library/header file

For this tutorial, we will create ;

  • Declare all your functions in myNM_tutorial.h

  • Define all your functions in myNM_tutorial.c

  • Include your library in main sourceC_createHeader_example.cpp

    Don't worry about the file extension of *.cpp or *.c

    You can use either extension with Visual Studio for Numerical Programming course


Step 1. Workspace Folder

Create local directory for programming

We will create the main directory under

C:\Users\yourID\source\repos

e.g. C:\Users\ykkim\source\repos

You can search for 'repos' in window menu

This is where your assignment projects should be located.

For this tutorial, let us create the new workspace directory as

  • Name the directory as "NP"

    A name that clearly shows the course name

Create more necessary sub directories

Example:

  • C:\Users\yourID\source\repos\NP

  • C:\Users\yourID\source\repos\NP\tutorial

  • C:\Users\yourID\source\repos\NP\include

Step 2. Create a tutorial C Project

Under \tutorial Directory, create a new folder named as TU_createheader

  • C:\Users\yourID\source\repos\NP\tutorial\TU_createheader

Create a new empty project in Visual Studio Community. Name the project as TU_createheader

Create a new C/C++ source file for main()

  • Name the source file as C_createHeader_example.cpp

#include "stdio.h"
#include "stdlib.h"

// Include path will be included at the end of tutorial
// #include "myNP_tutorial.h"


void printVec(double* vec, int row);

int main(int argc, char* argv[])
{

	double x[3] = { 1, 2, 3 };
	int x_size = sizeof(x)/sizeof(double);

	printVec(x, x_size);

}


void printVec(double* vec, int size)
{
	for (int i = 0; i < size; i++)
		printf("Vector[%d] = %.1f \n", i, vec[i]);
	printf("\n");
}

Step 3. Create your Header files

Under the directory of \include, create 'myNP_tutorial.cpp' and 'myNP_tutorial.h'.

  • C:\Users\yourID\source\repos\NP\include

Do not make duplicate copies of these files in your local drive. Update these files as you do assignments.

#ifndef		_MY_NP_H		// use either (#pragma once) or  (#ifndef ...#endif)
#define		_MY_NP_H

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

extern void printVec(double* vec, int row);

#endif
// Change the directory path
#include "../include/myNP_tutorial.h"

void printVec(double* vec, int size)
{
	for (int i = 0; i < size; i++)
		printf("Vector[%d] = %.1f \n", i, vec[i]);
	printf("\n");
}

Step 4. Include your Header files

In the above main() program, include your header library by finding the path.

Now, you need to delete the function definition of printVec() in main(), for we have included the function from the header library file.

The main source file should be modified as

/*  C_createHeader_example.cpp  */

#include "stdio.h"
#include "stdlib.h"

// Change the Include path 
#include "../../../include/myNP_tutorial.h"   // Find the location of header files
// #include "myNP_tutorial.h"   // if the PATH is already Included in Project


void printVec(double* vec, int row);

int main(int argc, char* argv[])
{

	double x[3] = { 1, 2, 3 };
	int x_size = sizeof(x)/sizeof(double);

	printVec(x, x_size);

}

// void printVec() definition is deleted in this file

Compile and run the program.

PreviousInstalling VS Code(Mac/Linux)NextPointer

Last updated 9 months ago

Was this helpful?

image

Paste the following code or

download src file from here
You can download source files here