Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 relationship between library files and .cpp files
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chopficaro




PostPosted: Thu Sep 11, 2008 10:19 pm   Post subject: relationship between library files and .cpp files

ive got to start making my own libraries and using them, but i have no idea where to start. i know a tiny bit about them, like theres one type of file with all the function paramaters, and one type of file with all the function definitions, and one type of file that may have main it it, but im confusing the c++ and c syntax between these and ive got to learn this stuff fast.

can u point me in the right direction?
anyone got any links?
something for me to read?
tutorial videos?

any help would be greatly appreciated.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Sep 11, 2008 11:34 pm   Post subject: RE:relationship between library files and .cpp files

Generally speaking a head file contains non-executable code. It essentially explains to the linker the interfaces for functions and classes so that files can be statically type checked before linking takes place.

For instance, foo.h:

code:
int foo(int bar);


The include directive simply does a text substitution, so when I include it in something like:

code:
#include "foo.h"

int main()
{
   int baz = foo(42);
}


Now when I compile the file with my main function in it the compiler knows foo exists, and how it relates to the rest of the program.
chopficaro




PostPosted: Fri Sep 12, 2008 10:20 pm   Post subject: RE:relationship between library files and .cpp files

so, i got a project,

main is in main.cpp

i have a bunch of functions that are used everywhere, the declarations in func.hpp, and the definitions in func.cpp.

at the top of main.cpp, and in func.cpp, i have #include "func"

when i compile the project, there are only 2 files under the word "project": main.cpp and func.cpp, HOWEVER, i do have func.hpp in the same directory

do i have it right so far?

if so, heres my beef:

in the past i have included other files such as iostream, but i have never had, under the word "project", the file iostream.cpp
OneOffDriveByPoster




PostPosted: Fri Sep 12, 2008 11:20 pm   Post subject: Re: RE:relationship between library files and .cpp files

chopficaro @ Fri Sep 12, 2008 10:20 pm wrote:
so, i got a project
So you are using some sort of IDE?

In any case, <iostream> does not have to be a real file. Also, your implementation of the functions defined in the header files does not have to be all in one file. If you have "header.hpp", you can have some of your function definitions in "a.cpp" and some in "b.cpp". Last but not least, your <iostream> is probably a real file, and it probably has the definitions in there too. The "inclusion model" is one way that templates in C++ can be used and having the template definitions in the header file is how most people do it.
md




PostPosted: Sat Sep 13, 2008 12:24 am   Post subject: RE:relationship between library files and .cpp files

your include in main.cpp would be "func.hpp".
chopficaro




PostPosted: Sat Sep 13, 2008 8:30 am   Post subject: RE:relationship between library files and .cpp files

thanx 4 ur help, now i just have to figure out the orthodox syntax right now

i thought that in c++, u weren't supposed to type the file extention into the include file names anymore, like stdio.h becomes cstdio

is this not true for the files i make myself?
md




PostPosted: Sat Sep 13, 2008 9:14 am   Post subject: RE:relationship between library files and .cpp files

Right, for standard library includes you do not include the .h, but for your own files (and most third party libraries) you use the full file name, and a relative path if the files are in different directories.
Rigby5




PostPosted: Thu Oct 09, 2008 10:56 pm   Post subject: Re: RE:relationship between library files and .cpp files

chopficaro @ Fri Sep 12, 2008 10:20 pm wrote:
so, i got a project,

main is in main.cpp

i have a bunch of functions that are used everywhere, the declarations in func.hpp, and the definitions in func.cpp.

at the top of main.cpp, and in func.cpp, i have #include "func"

when i compile the project, there are only 2 files under the word "project": main.cpp and func.cpp, HOWEVER, i do have func.hpp in the same directory

do i have it right so far?

if so, heres my beef:

in the past i have included other files such as iostream, but i have never had, under the word "project", the file iostream.cpp



When you compile your main.cpp, it makes a main.o or main.obj, and when you link all the parts of that together, it produces main.exe.
But someone has already compiled iostream.cpp into iostream.obj, and then linked it differently in order to make a iostream.lib.
So when you link your main together, it adds in the iostream.lib.

You may be using something that combines the steps of compiling and linking together, but it is good to understand the difference.
Compiling turns a single file into a machine form, but linking combines references from many files into something that can actually run.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: