LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to compile files together in Linux (https://www.linuxquestions.org/questions/programming-9/how-to-compile-files-together-in-linux-4175472103/)

atlantis43 08-04-2013 08:29 PM

how to compile files together in Linux
 
I'm currently using a text to learn C programming, in which an instructive program (below) uses separate files, but talks only of Unix & DOS environments for how to compile them together. Wondering if anyone can explain how to "compile them together" in the Unix environment (using clang compiler)? All 3 files are in the same directory.
>>>>>>>>>>>>>>>
PROGRAM 9.9
Code:

/* usehotel.c -- room rate program */
/* compile with  Listing 9.10      */
#include <stdio.h>
#include "hotel.h" /* defines constants, declares functions */

int main(void)
{
  int nights;
  double hotel_rate;
  int code;

  while ((code = menu()) != QUIT)
  {
      switch(code)
      {
      case 1 : hotel_rate = HOTEL1;
              break;
      case 2 : hotel_rate = HOTEL2;
              break;
      case 3 : hotel_rate = HOTEL3;
              break;
      case 4 : hotel_rate = HOTEL4;
              break;
      default: hotel_rate = 0.0;
              printf("Oops!\n");
              break;
      }
      nights = getnights();
      showprice(hotel_rate, nights);
  }
  printf("Thank you and goodbye.");
 
  return 0;
}

PROGRAM 9.10
Code:

/* hotel.c -- hotel management functions */
#include <stdio.h>
#include "hotel.h"
int menu(void)
{
    int code, status;

    printf("\n%s%s\n", STARS, STARS);
    printf("Enter the number of the desired hotel:\n");
    printf("1) Fairfield Arms          2) Hotel Olympic\n");
    printf("3) Chertworthy Plaza        4) The Stockton\n");
    printf("5) quit\n");
    printf("%s%s\n", STARS, STARS);
    while ((status = scanf("%d", &code)) != 1  ||
            (code < 1 || code > 5))
  {
        if (status != 1)
            scanf("%*s");
        printf("Enter an integer from 1 to 5, please.\n");
    }
 
    return code;
}

int getnights(void)
{
    int nights;

    printf("How many nights are needed? ");
    while (scanf("%d", &nights) != 1)
    {
        scanf("%*s");
        printf("Please enter an integer, such as 2.\n");
    }
 
    return nights;
}

void showprice(double rate, int nights)
{
    int n;
    double total = 0.0;
    double factor = 1.0;

    for (n = 1; n <= nights; n++, factor *= DISCOUNT)
        total += rate * factor;
    printf("The total cost will be $%0.2f.\n", total);
}

PROGRAM 9.11
Code:

/* hotel.h -- constants and declarations for hotel.c */
#define QUIT      5
#define HOTEL1    80.00
#define HOTEL2  125.00
#define HOTEL3  155.00
#define HOTEL4  200.00
#define DISCOUNT  0.95
#define STARS "**********************************"

// shows list of choices
int menu(void);

// returns number of nights desired
int getnights(void);

// calculates price from rate, nights
// and displays result
void showprice(double rate, int nights);


Ser Olmy 08-04-2013 09:06 PM

What you have here are two .c files and a header file that are parts of the same program.

The third file is supposed to have the filename "hotel.h" - it even says as much in the comments. It is pulled in by the #include "hotel.h" statement in the first file.

The second file by itself is not a program at all, as it lacks the "main" function. It's pretty obvious that it belongs to the same program as the first file, as it contains functions that are used but never defined in the first file.

Either #include the second file in the first, or just join the two files together with cut and paste as the comment in the first file suggests.

atlantis43 08-04-2013 10:27 PM

Sorry, but that doesn't seem to jibe with the text discussion, where they speak of such things as a command line such as "gcc file1.c file2.c for Unix compilers.
In this case, there are 2 separate .c files (and the .h file). The text discusses the use of putting the functions related to the driver in a separate file, and then putting constants, etc in a separate header file.

konsolebox 08-04-2013 11:15 PM

One basic way to do it is:
Code:

gcc -c -o usehotel.o usehotel.c
gcc -c -o hotel.o hotel.c
gcc -o usehotel usehotel.o hotel.c

Here are some tutorials:
http://www.linuxquestions.org/linux/...grams_on_Linux
http://pages.cs.wisc.edu/~beechung/ref/gcc-intro.html

And some would actually prefer to use ld like many open-source softwares. I did prefer it before.
http://stackoverflow.com/questions/5...utable-with-ld

NevemTeve 08-05-2013 02:57 AM

> they speak of such things as a command line such as "gcc file1.c file2.c for Unix compilers.

That works perfectly well, try this:

Code:

gcc -o hotel hotel.c usehotel.c
then enter ./hotel to execute the program

atlantis43 08-05-2013 05:01 AM

Yes! Tutorials very helpful. Are gcc compilers present in Unix & Linux?
How does clang differ from gcc?

chrism01 08-06-2013 02:22 AM

gcc compiler definitely exists in Linux: g=gnu, as in GNU/Linux :)


All times are GMT -5. The time now is 11:45 AM.