Search code examples
clinker-errorsundefined-referencemulti-module

undefined reference to function call


I am new to C language. I have an assignment, in which I have to make use of multi-module concept to compile a program. I keep getting 'undefined reference' error. The program simply converts farenheit to celsius; if I use only one script which includes function prototype,call and definition it simply works, but with multi-module concept, it keeps yielding 'undefined reference' error. Do I have to put all the three files in different or same folder?

conversion.c:

/*
* filename: conversion.c
* Purpose: The file contains function prototype
* to convert farenheit to celsius 
*/

#include "conversion.h"
//conversion for farenheit to celsius
float convertTemp(float tmp)
{
    return ((tmp-32)*0.555);
}

conversion.h:

/*
* FILENAME:  conversion.h
* PURPOSE: The file contains function prototype
* for conversion.c
*/
float convertTemp(float tmp);

convert_driver.c:

/*
* FILENAME:  convert_driver.c
* PURPOSE:   The file contains main() function
* and user interface.
*/
#include <stdio.h>
#include "conversion.h"
int main(void)
{
    float x,y;
    printf("Please enter your temperature for conversion:  \n");
    scanf("%f", &x);
    y = convertTemp(x);
    printf("Converted Temperature: %0.2f\n", y);
    return 0;
}

I keep getting error for the line:

    y=convertTemp(x)

Any help would be appreciated.


Solution

  • The problem is with linking compiled objects.

    Try manually compiling your sources like this:

    gcc -o convert_driver convert_driver.c conversion.c
    

    In this way, after compilation, the compiler knows what to link.

    You can also manually compile sources into objects, and use a static linker to generate the executable:

    gcc -c -o convert_driver.o convert_driver.c
    gcc -c -o conversion.o conversion.c
    ld -o convert_driver convert_driver.o conversion.o
    

    For a typical IDE, check if there's a "Create Project" option. Multi-file support usually comes within the concept of "a project".