-
January 18th, 2010, 12:52 PM
#1
linking files/ make(?)
hello all! i am new here and pretty junior as a developer so i would like your help please
i have created a project which is consisted of around 30 C++ files. every .cpp file contains one class and some methods for this specific class. if we put the 30 files in some order, every file depends on the previous one and to do that so far i have included the following:
#include "previous_file.cpp" in every file, so every file includes the previous one until the last one.
So in the end, i am running the last file which includes in the way i mentioned above the one before that, which includes the one before that and so on and i finally includes them all. For some reason that i dont understand i know that this is not the best solution of linking the files, so i would like some help doing that. I am working on a linux environment if that's important to you.
So first of all, i would like some help linking the files and if you could briefly explain to me why my way of linking them is not appropriate i would really appreciate it.
Secondly, could i use the tool "make" to build my project?
P.S i am not looking for easy answers and i would be more than glad to read any kind of instructions etc concerning my questions if you can provide me with a link. thank you in advance
-
January 18th, 2010, 12:56 PM
#2
Re: linking files/ make(?)
Makefiles are a good solution, but their syntax is unintuitive at best.
In general, you should never #include a .cpp file. There's a bit of an exception when working with templated code, but often you'll see the implementation file named ".inl" in that case (for inline) rather than .cpp, to avoid confusion.
Each cpp files should be compiled separately, and then the resulting object files should be associated by the linker.
-
January 18th, 2010, 06:42 PM
#3
Re: linking files/ make(?)
how can i compile each file separately?for example file no2 needs the class which is declared in file no1 because it creates an object of this class and then it uses it ...can i include it some how with an external declaration? like in C where we can write for example "extern int k;" if k is declared is some other file? can i do the same thing with the classes?thx
-
January 18th, 2010, 06:43 PM
#4
Re: linking files/ make(?)
Read up on header files. That's what they're for.
-
January 19th, 2010, 12:27 AM
#5
Re: linking files/ make(?)
You should include the header (.h) files and not the code (.cpp) files within another code (.cpp) file. Header (.h) file should have the forward declarations of classes and methods.
-
January 29th, 2010, 05:56 AM
#6
Re: linking files/ make(?)
thank you all for your answers....
i tried to do what you suggested. instead of including the whole 1st file to the 2nd i made a header file that contains only the declarations of the first file. i did it like that
---------------
1st.cpp
---------------
class 1st {
private:
................
................
public:
..............
...............
};
implementation of 1st method;
implementatoin of 2nd method; etc etc
-----------------------
1st.h
----------------------
#ifndef _1st
#define _1st
class 1st{
private:
...............
...............
public:
..............
...............
};
#endif
and i included the .h file in the 2nd file. but when i am trying to compile the 2nd file i get many errors for undifined references...any help? thx
-
January 29th, 2010, 06:11 AM
#7
Re: linking files/ make(?)
i wasnt using -c in the command, it is working now
thx for your time
-
January 29th, 2010, 10:51 AM
#8
Re: linking files/ make(?)
kindly excuse me if this is repetitive, but just thought I might mention it just in case:
Intro to different files
--------------------------
Header File - Contains the specification (has an extension .h usually)
Implementation File - Contains the implementation of the header file. (usually same file name as the header file but with an extension as .cpp)
Application File - This file uses the classes/functions whose spec was available in the header file. (usually has the extension .cpp, a different filename from that of the header and implementation files)
Object File - Compilation of a file creates an object file, and all object files can be compiled to form the executable file.
If you separate them into different files, helps in better readability and less dependancy.
Whenever a spec of a class/function is used, then the header file containing spec/function should be included (using #include).
Compilation (of non-templates)
--------------------------------------
1) The implementation file needs to be compiled to create an object file. (only the header file needs to be included, cause the spec is the only thing needed to compile)
2) The application file needs to be compiled to create an object file. (only the header file needs to be included, cause the spec is the only thing needed to compile)
Linking - Then these object files should be linked to create the executable file.
Templates:
--------------
Templates don't require compiling, I found the following link useful, it explains why (it talks about visual studio, but the same holds good for the rest as well):
http://www.ecs.fullerton.edu/~sowell...teClasses.html
Makefiles:
--------------
I use makefiles, its not very complicated at least to the extent I use it.
Refer
http://www.sethi.org/classes/cet375/...mpilation.html
http://myweb.stedwards.edu/laurab/help/g++help.html
http://mrbook.org/tutorials/make/
http://www.codeguru.com/forum/showth...ht=application
In make files, you need to mention
1) the dependencies,
2) command to compile and link
3) clean up - what files to delete at the start compilation, like removing previously created object files and executable files.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|