Thread: Creating header files and includes

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    Creating header files and includes

    I'm attempting to create a function called Greetings, where I put it in a separate file. I followed a tutorial on youtube, but it kept giving me errors. Below is the source code for the main.cc, Greetings.cc, and Greetings.h. Though when I tried compiling it through my Mac's terminal it ran quite fine, but when I tried running it inside my text editor (Sublime Text 3), it keeps on giving me these weird errors, long ones. Does anyone see anything wrong?

    main.cc
    Code:
    #include <cstdio>
    #include "Greetings.h"
    using namespace std; 
    
    int main() {
        Greetings bo;
        return 0;
    }
    Greetings.cc
    Code:
    #include <cstdio>
    #include "Greetings.h"
    
    Greetings::Greetings() {
        printf("Hello, how are you?\n");
    }
    Greetings.h
    Code:
    #ifndef GREETINGS_H 
    #define GREETINGS_H 
    
    class Greetings {
    public:
        Greetings();
    };
    
    
    #endif

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you using C-stdio functions instead of the safer C++ streams?

    What exactly are your errors?

    Did you add "Greetings.cc" to your project?

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    Okay, I changed it to #include <iostream>, but it still gave me errors.

    When I tried running the main.cc within sublime text 3, it gave me this error
    Undefined symbols for architecture x86_64:
    "Greetings::Greetings()", referenced from:
    _main in main-d6c18e.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    [Finished in 0.7s with exit code 1]
    [shell_cmd: g++ "/Users/conrados/Desktop/C++ Programs/Greetings/main.cc" -o "/Users/conrados/Desktop/C++ Programs/Greetings/main" && "/Users/conrados/Desktop/C++ Programs/Greetings/main"]
    [dir: /Users/conrados/Desktop/C++ Programs/Greetings]
    [path: /usr/bin:/bin:/usr/sbin:/sbin]
    Last edited by asilvester635; 01-27-2017 at 10:45 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > [shell_cmd: g++ "/Users/conrados/Desktop/C++ Programs/Greetings/main.cc" -o "/Users/conrados/Desktop/C++ Programs/Greetings/main" && "/Users/conrados/Desktop/C++ Programs/Greetings/main"]
    Your build command is wrong.
    This tries to compile and link a complete program in one step.

    When you have multiple source files, you typically need to do
    g++ -c main.cc
    g++ -c Greetings.cc

    and so on for every source file in your project.

    Then at the end, you do
    g++ -o program main.o Greetings.o
    adding the whole list of .o files you compiled individually.

    The way you organise all this for larger projects is by using 'make' files.


    With just two files, you could brute-force it with
    g++ "/Users/conrados/Desktop/C++ Programs/Greetings/main.cc" "/Users/conrados/Desktop/C++ Programs/Greetings/Greetings.cc" -o "/Users/conrados/Desktop/C++ Programs/Greetings/main"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating header files
    By piczim in forum C Programming
    Replies: 2
    Last Post: 07-04-2013, 12:07 AM
  2. Creating header files
    By histevenk in forum C++ Programming
    Replies: 14
    Last Post: 10-17-2007, 08:02 PM
  3. Creating Header Files
    By Neo1 in forum C++ Programming
    Replies: 30
    Last Post: 09-30-2006, 03:26 PM
  4. creating header files
    By GamingMarvel in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2005, 06:57 AM
  5. Includes in header files?
    By Leiken in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 12:04 PM

Tags for this Thread