Search code examples
c++linkerprojectcodeblocksheader-files

How can I link 2 files in CodeBlocks without having a project?


I want to use headerfiles (header.h and header.cpp), but I'm not using a project. Is there any way to link these files together, or is making a project the only solution?

The problem I encountered right now:

main.cpp

#include "class.h"

int main()
{
    MyClass test;
}

class.h

class MyClass
{
public:
    int x;
    MyClass();
};

class.cpp

#include "class.h"

MyClass::MyClass() : x(0) {}

ERROR: undefined reference to `MyClass::MyClass()'


Solution

  • If you are not using a project, then you can't compile it using CodeBlocks. You can compile the code manually, using g++, for example, would be like this: g++ -o myprog main.cpp class.cpp. If you are using another compiler, then you will have to look for the command for it.