0

I receive the following error when I try to compile three c++ files with g++ main.cpp. If I combine them in one file, it works.

main.cpp:(.text+0x10): undefined reference to `Time::Time()'

Time.cpp

#include <iostream>
#include "Time.h"
using namespace std;

Time::Time()
{
    a=5;
}

Time.h

#ifndef TIME_H
#define TIME_H

class Time {

public:
Time();
private:
int a;
};
#endif

main.cpp

#include <iostream>
#include "Time.h" 
using namespace std;


int main()
{
    Time t;
}
2

1 Answer 1

3

You need to compile all the CPP files because each one is a separate compilation unit

g++ main.cpp Time.cpp -o main

For more information about that read

Sign up to request clarification or add additional context in comments.

3 Comments

is there a shortcut? what about if I have more than 100 files?
if you have that many files then you must create a makefile, otherwise you'll end up compiling a file again and again. In MSVC or some other IDEs just create a new solution/project and the IDE will handle the compilation for you
You could use shell expansion, e.g. g++ *.cpp -o main

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.