main.cpp functions to separate .cpp

I have written some utility functions in my own small project and now have too many functions to keep tidy, and also need to use them in other parts of the projects.

instead of copying them over and end up with a mess, is it possible to just do something like this?

#include "utility.functions.h"

I then compile with

g++ main.cpp util.cpp
TroudeloupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

coandaCommented:
where you put

#include "utility.functions.h"

did you mean to write?

#include "utility/functions.h"

if so then yes, you can do that. you can use a full path name (eg. ~/lib/utility.h) if you want to.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaime OlivaresSoftware ArchitectCommented:
yes, you can, it is the usual practice.
Name of header file is not important, it could be "utility.functions.h", but usually is the same name as util.cpp (util.h)
0
TroudeloupAuthor Commented:
#include "utility/functions.h"

that way I can use folders right?

that's cool.

what do I do with .h?
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

TroudeloupAuthor Commented:
Or rather, what do I put inside .h?
0
Jaime OlivaresSoftware ArchitectCommented:
You put the prototype declarations of the functions you implement in util.cpp
Also you can put some required #defines or constants if needed.
0
TroudeloupAuthor Commented:
i have only one .h and it was about a class..

can you post a working .h for this?

#include <iostream>

using namespace std;

int main()
{
    cout << "hello" << endl;
   
    return 0;

}
0
Jaime OlivaresSoftware ArchitectCommented:
main.cpp
======

#include <iostream>   // it should be iostream.h
#include "util.h"

using namespace std;

int main()
{
    // use your class here
    return 0;

}

util.h
====

class YourClass
{
    public:
        SomeMethod();  // just declare it
}

util.cpp
=====

void YourClass::SomeMethod()
{
     // implement the method here
}
0
Infinity08Commented:
>> #include <iostream>   // it should be iostream.h

No !! It should be <iostream> :)


The example was also a bit incomplete and had a few typo's, so here's a working version :
/* ---- main.cpp ---- */
 
#include <iostream>              // <--- <iostream> is correct !!
#include "util.h"
 
using namespace std;
 
int main() {
    YourClass yc;
    yc.SomeMethod();             // <--- we use the class here
    return 0;
}
 
 
/* ---- util.h ---- */
 
#ifndef UTIL_H                   // <--- add include guard to avoid including the header more than once
#define UTIL_H
 
class YourClass {                // <--- this is just the class definition - no implementation here !!
    public :
        void SomeMethod();       // missing void
};                               // missing ;
 
#endif /* UTIL_H */
 
 
/* ---- util.cpp ---- */
 
#include "util.h"                // <--- include the header here !!
 
void YourClass::SomeMethod() {   // <--- and here comes the implementation of the class
     // implement the method here
}

Open in new window

0
Infinity08Commented:
And you can do the same if you just have some functions like :


/* ---- main.cpp ---- */
 
#include <iostream>
#include "util.h"
 
using namespace std;
 
int main() {
    someFun();                   // <--- we can call the functions here
    int a = someMoreFun(5);
    return 0;
}
 
 
/* ---- util.h ---- */
 
#ifndef UTIL_H                   // <--- add include guard to avoid including the header more than once
#define UTIL_H
 
void someFun();                  // <--- just the function prototypes here
int someMoreFun(int i);
 
// etc ...
 
#endif /* UTIL_H */
 
 
/* ---- util.cpp ---- */
 
#include "util.h"                // <--- include the header here !!
 
void someFun() {                 // <--- the function implementations go here
    // do something fun
}
 
int someMoreFun(int i) {
    // do something more fun
    return i;
}
 
// etc ...

Open in new window

0
Infinity08Commented:
coanda's (20847893), jaime_olivares's (20847991, 20848038), and mine (20848494) together answer the question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.