Sign in to follow this  

SSH and compiling multiple .cpp files

This topic is 4091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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??

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4091 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this