c++linuxfltk

FLTK - Compile Multiple .cpp's - Linux


For simplicity, I have reduced my problem to a minimal working example.

I begin with a file, blah.cpp, that is very simple.

#include <FL/Fl.H>
#include <FL/Fl_Window.H>

int main(int argc, char **argv) {
    Fl_Window* win = new Fl_Window(100,100,100,100,"Title");
    win->show();
    return Fl::run();
}

To compile it, I type into my terminal (while in the directory of the file blah.cpp)

fltk-config --compile blah.cpp

Which works perfectly fine. I simply type ./blah, and the 100 pixel window is shown on the screen. However, my issue arises once I would like to expand to more than one file - namely, blah.cpp,head.h,and blah2.cpp.

/* head.h */
#ifndef HEAD
#define HEAD

#include <FL/Fl.H>
#include <FL/Fl_Window.H>

class My_Window{
    public:
        My_Window(int,int);
        bool open();
        ~My_Window(){ 
            delete win;
        }
    private:
        Fl_Window* win;
};

#endif

/* blah2.cpp  */
#include "head.h"

My_Window::My_Window(int w, int h):{
    win = new Fl_Window(w,h);
    win->show();
}

bool My_Window::open(){
    return Fl::run();
}

/* blah.cpp */
#include "head.h"

int main() {
  Window win(300,300);
  return win.open();
}

My question is, how would I compile this project (blah.cpp and blah2.cpp) with the terminal? My initial guess would be to place everything in the same directory and do something like

fltk-config --compile *.cpp

or

fltk-config --compile "*.cpp"

but neither of those work. How would I go about compiling this?

[SOLUTION]

I ended up solving this right after I finished typing it. I'll leave it up since I've seen it appear numerous times on the internet.

I did away with the fltk-config command, and stuck with the regular g++:

g++ -std=c++11 blah.cpp blah2.cpp -o blah -lfltk

And with that it worked! The executable of the program was then created with the title blah! Of course, the -std=c++11 line only means to follow the c++11 standard, so that isn't completely necessary for my case.


Solution

  • fltk-config --compile <file.cc> is made to be a convenience option when you have a single-file project. It compiles and links your application.

    If you have multiple files, say main.cc and account.cc your best option is to do the compilation first, and then link them all together.

    Something like (note I have requested the images library too):

    g++ `fltk-config --cxxflags` main.cc
    g++ `fltk-config --cxxflags` account.cc
    gcc -o myapp `fltk-config --use-images --ldflags` main.o account.o