SSH and compiling multiple .cpp files

Started by
5 comments, last by Palidine 17 years, 6 months ago
I am using SSH to connect to my school's UNIX account. I wrote a very simple C++ program that works with an external class file and displays some output. The file structure is as follows: (1) driver.cpp --> where main() is located (2) Time.h --> the header file for my standard/military clock converter (3) Time.c --> the class file for my clock converter I made sure to #include "Time.h" in BOTH my .c and .cpp files. They compile fine in Dev C++ on my desktop, and the program works without any problems. Using SSH and pico, I created three new files inside the same directory, each with the same names as the files listed above. Then I just cut/pasted all my Dev C++ files to my new UNIX files. When I type in the following command: g++ *.cpp I get about 30 different errors saying that I have "undefined references" to all of my Time class's methods. What is going on -- how do I fix this??
Well I believe in God, and the only thing that scares me is Keyser Soze.
Advertisement
Change the suffix of your time file to time.cpp and that command should work. Otherwise, you can compile each to an object file seperatly by using the -c option and than link them toghether.

g++ -c driver.cpp -o driver.o
g++ -c time.c -o time.o
g++ driver.o time.o -o nameOfProgram
Just so you know, the discrepancy has nothing to do with the fact that you are using SSH or logging-in remotely. It is because your school uses an entirely different development environment. For one thing, DEV C++ and the unix g++ are totally different compilers. It's not always simple to get a program that compiles on one to immediately compile on the other.
Quote:Original post by hisDudeness
driver.cpp
Time.c

g++ *.cpp


Do you see the problem now? When you invoke g++ from the command line, you're supposed to give it a list of all the files that are "roots" for the translation units (i.e. normally, all the non-header files). '*.cpp' means "all files in the current directory whose names end in '.cpp'. Time.c does not end in .cpp, therefore it didn't get compiled. Therefore there is no Time.o, therefore the linker can't find the functions that Time.o is supposed to contain, therefore linker errors.

Edit: So yeah, it's like xidis said. Kelly G, that's true in general, but usually a relatively minor consideration, and irrelevant here (except for the correct assertion that doing the work remotely via SSH doesn't matter).
You could make a Makefile (despite the fact that they are extremely evil). How to do so is outside of the scope of this post :)

Mixing C and C++ in the same program is not something that you should normally do (although you can). Personally, I'd write the whole thing in C++, it will make things easier later.

Mark
you could also adjust the Makefile.win that Dev-C++ generates and change it to match your schools paths and such
Yes, if you're compiling a multiple-file project with g++ you really really do want to be using Makefiles. They're confusing as hell when you first start learning them but they are an incrediably powerful tool for command line *nix development. google around for makefiles for some tutorials.

As pointed out, just because something compiles on a PC does not mean it will compile on *nix. some libaries, depending on what you are doing, can be completely different and use entirely different function calls to arrive at the same functionality.

-me

This topic is closed to new replies.

Advertisement