0

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.

8
  • The folder you put your files has little importance as long as you know where your files are. It sounds like you are not properly compiling your files together. If all your files are in the same folder, simply do gcc *.c, otherwise you can fetch your files individually and directly with something such as gcc first_file.c /tmp/somewhere/second_file.c [...]. In your specific case, considering your files are in the same folder, you'd have to do: gcc convert_driver.c conversion.c. Commented Jul 1, 2018 at 7:32
  • I am using codelite, where do I type those commands actually? Commented Jul 1, 2018 at 7:37
  • I am not familiar with this IDE, but it should automate the line I previously gave you. You may want to have a look in the compilation settings of CodeLite. Commented Jul 1, 2018 at 7:39
  • 1
    That is a linker-error. it means you have not compiled all object files together. You can solve the problem by compiling with gcc -Wall -Wextra -o convert_driver convert_driver.c conversion.c (where -Wall -Wextra enable warnings which you should include every time you compile and do not accept code until it compiles without warning). You can also compile each to object individually (e.g. gcc -c -o conversion.o conversion.c same for convert_driver and then link with gcc -o convert_driver convert_driver.o conversion.o Commented Jul 1, 2018 at 8:20
  • Possible duplicate of run a program with more than one source files in GNU c++ compiler Commented Jul 1, 2018 at 9:14

1 Answer 1

2

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".

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

4 Comments

@SalN85 You would like to open a Command Prompt (Windows) or Terminal (macOS / Linux) and type there. Make sure you cd into the correct directory.
Note: the linking will take a few more parameters depending on where your crtbegin.o and ctrend.o and crtn.o files are (C-runtime). You are better served simply letting gcc handle the compile and link with gcc -o convert_driver convert_driver.c conversion.c (plus enabling-warnings in the original comment) And, yes, just open an xterm (or konsole in KDE or gnome-terminal in gnome or XFCE-terminal in XFCE or the like) and change to the directory containing your .c files and just paste gcc -o convert_driver convert_driver.c conversion.c at the command prompt (then ./convert_driver to run)
You can also let gcc set up the linker environment for you after you compile each source to object and then gcc -o convert_driver convert_driver.o conversion.o (to let gcc handle the C-runtime linker requirements) Otherwise where your crt... files are installed will depend on your distribution.
this answer fails to recompile the source files into object files (and relink) when the header file has changed. Suggest appending -I. to the compile statement(s)

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.