First of all, I'm a total noob on SO, so please go easy on me :)
That being said, my question might be really easy to answer:
How would I use sublime text 3 for c++ with multiple files (header/implementation files)
I've been trying some build systems provided by other people, this is the one I'm currently using and seems to work just fine for compiling single .cpp files but is telling me some errors which I don't understand
here is my build config using g++
{
"cmd": ["g++.exe", "-std=c++14", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
"shell": true,
"selector": "source.c++"}
here is my main.cpp:
#include <iostream>
#include <string>
#include "num.h"
using namespace std;
int main(){
Num n(35);
cout << n.getNum() << endl;
return 0;}
here is my num.h file:
#ifndef NUM_H
#define NUM_H
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
#endif
and here is my num.cpp:
#include "num.h"
#include <iostream>
#include <stdlib.h>
Num::Num(int x){
num = x;
}
int Num::getNum()
{
return num;
}
all files are in the same directory and whenever I'm using g++ on the command like like this: (g++ (tdm64-1) 5.1.0)
g++ main.cpp num.cpp
there is no problem and everything works just fine
but whenever I'm trying to build it with sublime text it throws me this error
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x1a): undefined reference to `Num::Num(int)'
C:\Users\GEBRUI~1\AppData\Local\Temp\ccqq94my.o:main.cpp:(.text+0x26): undefined reference to `Num::getNum()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.8s]
I would really appreciate it if someone could tell me what I'm doing wrong here :)
Once you start getting into the realm of needing to compile multiple files, it's really best to start using
make
orCMake
instead of trying to build the compile commands yourself on the command line or in a build system. This way you wouldn't have to edit your build system every time you added a new file to your project.There is a Make build system that comes with Sublime, but you will need to generate the
Makefile
externally, either by hand or using tools likeautoconf
andautomake
. TheCMakeBuilder
package looks like it would be useful for working with CMake (I haven't used it myself), although you can of course use external tools as well.