-3

Update: I ended up getting this work by using DLLs and loading and unloading them. https://www.youtube.com/watch?v=mFSv0tf6Vwc

I know its possible because I've seen posts about this but i don't really understand them.

So i have class(DetailsLayout) that calls a method in another class(Components). At runtime i change the contents of Components to add another component that the user created to that method. I want to recompile the DetailsLayout.cpp and the Components.h but i'm confused as to how to go about this.

right now i'm trying this because of this post : Using G++ to compile multiple .cpp and .h files

system(("g++ -c ProjectComponents.hpp").c_str());
system(("g++ -c DetailsLayout.cpp").c_str());
DetailsLayout::CreateMenu();

I get an error that says g++ is not recognized as an internal or external command.

as per Jesper's suggestion ill mention what id like my end goal to be. So right now in my Game Engine i allow users to add components (that i made) to objects in the scene dynamically. When i tried to make it so that the "user" can make a component and add it dynamically i couldn't do it unless i compiled the .h and .cpp file i made for them. If i manually include the file into my project and run the method for adding the class it works but i want that to happen when i click a button for compiling.

My Components all inherit from this class.

    class Component
{
public:
    Component();
    ~Component();

    virtual bool Initialize() { return true; }
    virtual bool Update(float dt) { dt; return true; }
    virtual bool Draw() { return true; }
    template <class T> T* GetSiblingComponent()
    {
        return m_owner->GetComponentByType<T>();
    }

protected:
    Entity* m_owner;
    char    m_name[Imgn::MAX_NAME_LEN];
    bool    m_enabled;
};
Andrew Velez
  • 89
  • 1
  • 10
  • Please note: The post you are linking, does not try to compile at runtime. Further, header needn't be compiled anyways. – m8mble Oct 24 '16 at 19:55
  • @m8mble Yea your right i meant to link this: http://stackoverflow.com/questions/11016078/is-it-possible-to-create-a-function-dynamically-during-runtime-in-c but i got to the g++ after some more "research". – Andrew Velez Oct 24 '16 at 20:07
  • 1
    You seem to be misunderstanding a few things; first and foremost that C++ is not an interpreted language, but a compiled one (where the compiled code is very much platform/hardware specific). – Jesper Juhl Oct 24 '16 at 20:21
  • Perhaps you should describe the end result you want to achieve. Then people could perhaps comment on how to actually reach the goal. Also; please don't use `system` - it has too many bad security implications. – Jesper Juhl Oct 24 '16 at 20:30
  • @JesperJuhl thanks for the feedback i added my end goal. I had been using system to open up .h and .cpp files in my program is there a better way to do it? – Andrew Velez Oct 24 '16 at 20:42
  • What is a component? How is it created? Is there a common interface? – m8mble Oct 24 '16 at 20:44
  • @m8mble ill add the interface to my question. – Andrew Velez Oct 24 '16 at 20:47
  • Ok, and what about something like `DetailsLayout::register_component(std::shared_ptr c)`? Your users can call that method if they want a new component instead of having to recompile. – m8mble Oct 24 '16 at 20:52
  • and just minor, that destructor needs to be virtual too. – m8mble Oct 24 '16 at 20:53
  • @m8mble that works and i do use that for Components i have created but i want there to be a way for user to create a Component while the program is running. i need to be able to include a file that didn't exist prior to running the program. – Andrew Velez Oct 24 '16 at 21:00
  • No, you just need a way of calling `::register_component` based on user input, ie. with a user defined `Component`. – m8mble Oct 24 '16 at 21:02
  • @m8mble this would work if i didn't need the class they make, i kind of see where your going with this but when a user creates a class inheriting from Component i sort of do reflection to know all its members so i can display them in the UI. – Andrew Velez Oct 24 '16 at 21:16
  • I'm not saying the current `Component` interface is the right thing. Maybe you need some special kind of component that user can click together. I can't answer that. Your solution though, where users provide the code, enables serious security concerns. What if my component formats your drive as a first step? You'd compile and run that thing freely. – m8mble Oct 24 '16 at 21:38
  • Adding new code on the fly is kinda tough to do reliably (and it is very hard to replace already running code), and frankly your question and comments suggest you are not really equipped to do that. Here is a cppcast story on a project which does something pretty similar: http://cppcast.com/2016/05/doug-binks/ . I strongly recommend you listen to it, read relevant shownotes, watch linked videos, etc before trying to hack around something. – Andrey Turkin Oct 25 '16 at 02:47

1 Answers1

0

I get an error that says g++ is not recognized as an internal or external command.

Well, it's not guaranteed that GCC is installed at your target machine. You have to ensure that in first place if you want to compile and run your code at arbitrary host machines.

Also you seem to miss the linking stage, and calling the resulting executable as well.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Ahh so ill download GCC i wan't aware. as for the linking and running the executable, i have my program running so i was kind of hoping the .cpp file would just update in the program. Sort of how UE4 does a hot reload when you click the compile button. – Andrew Velez Oct 24 '16 at 20:10
  • @AndrewVGaming For _hot replacement_ better look at languages like [eiffel](http://wiki.c2.com/?EiffelLanguage). – πάντα ῥεῖ Oct 24 '16 at 20:21
  • im aware it'd be easier to do using a language like lua or eiffel but i want my game engine to allow you to add components using C++. This a research project for college, so i don't mind working extra to get it to work. If Unreal can do it so can i :) – Andrew Velez Oct 24 '16 at 20:33
  • @AndrewVGaming Could you elaborate, what you mean by "add components" using C++? I'd be quite certain that this is achievable without any recompilations... – m8mble Oct 24 '16 at 20:42
  • @m8mble if your familiar with Unity my components would be like theirs. an example of one of my components would be SpatialComponent it holds the location,rotation and scale of an object. – Andrew Velez Oct 24 '16 at 20:45
  • 1
    I barely am, but as your example unity proves, this is all possible by means of proper class design. Unity doesn't recompile your code at runtime. – m8mble Oct 24 '16 at 20:48
  • Unlikely that Unreal compiles C++ code on the fly. More likely it defines a whole family of parameterized classes. – Peter Oct 24 '16 at 21:28