LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2013, 08:29 PM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
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);
 
Old 08-04-2013, 09:06 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,334

Rep: Reputation: Disabled
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.
 
Old 08-04-2013, 10:27 PM   #3
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-04-2013, 11:15 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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
 
1 members found this post helpful.
Old 08-05-2013, 02:57 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> 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
 
2 members found this post helpful.
Old 08-05-2013, 05:01 AM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Yes! Tutorials very helpful. Are gcc compilers present in Unix & Linux?
How does clang differ from gcc?

Last edited by atlantis43; 08-05-2013 at 05:06 AM.
 
Old 08-06-2013, 02:22 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,355

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
gcc compiler definitely exists in Linux: g=gnu, as in GNU/Linux
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Eglibc cannot compile suffix of object files while trying to cross compile Bry6n Linux From Scratch 0 08-21-2012 06:14 PM
Linux header files missing and v4l files unable to compile szutshi Linux - Newbie 5 09-13-2011 02:00 PM
CLFS 5.9 - Glibc compile: suffix of object files, cannot compile Noddegamra Linux From Scratch 3 04-27-2009 12:49 AM
Compile Files In Linux LinLove Linux - Newbie 13 04-28-2006 03:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration