including .cpp files

This is a discussion on including .cpp files within the C++ Programming forums, part of the General Programming Boards category; I'm using multiple classes that basically just interact with vectors of each other. I'm trying to test it, but I ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    including .cpp files

    I'm using multiple classes that basically just interact with vectors of each other. I'm trying to test it, but I keep getting the compiler errors "undefined reference to " for my Bathroom constructor and functions.

    However, when I change the #includes from .h to the .cpp files, the program compiles and works fine. But it seems to me like it's bad practice to include .cpp files over .h files. Is this true? Or is it no big deal? I don't think I've ever included a .cpp file. I recently switched to Linux...could that be an issue somehow?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    It's a bad practice.

    You need to compile/link all files together.

    g++ a.cpp b.cpp c.cpp

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    try extern in your .h file.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    No need for extern if he includes the header file, as per the convention.

    There's (usually) no need to declare the same thing in different places.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    s

    I tried running g++ <list of all .cpp files>, but nothing happened. Like the terminal just reset to waiting for me to put a command in. Is there a way to do this in the Code::Blocks IDE? I am used to Windows (I originally had this program on Windows working fine, now I'm trying to remake it), so I am a little new to using Linux. Is there is anything else I need to do (or could do) other than go to the terminal and do g++ .cpp files?

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    I tried running g++ <list of all .cpp files>, but nothing happened. Like the terminal just reset to waiting for me to put a command in.
    GCC only outputs something if there were errors/warnings.

    No output means everything was fine.
    Code:
    ./a.out
    to run it.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Sorry but I don't see what you mean by ./a.out. I tried "path/mainfile.cpp.out" and "g++ path/mainfile.cpp.out" and "/path/mainfile out" and other ones...what do you mean exactly?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    GCC produces a binary called "a.out" by default if you don't specify the output file name.

    Try "cd"ing into the directory that has all your cpps, do
    g++ a.cpp b.cpp c.cpp...
    then
    ./a.out

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Thank you so much

  10. #10
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Or just do:
    Code:
    g++ a.cpp b.cpp c.cpp -o myexecutable
    ./myexecutable
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    It works in C::B too. Just add all your source files to the project. It should compile and link them all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. .cpp and .h files - organization
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2006, 10:40 PM
  4. Replies: 4
    Last Post: 12-14-2005, 01:21 PM
  5. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 02:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21