How to setup sublime 3 build for C++ with multiple files

959 views Asked by At

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 :)

2

There are 2 answers

6
MattDMo On

Once you start getting into the realm of needing to compile multiple files, it's really best to start using make or CMake 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 like autoconf and automake. The CMakeBuilder 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.

0
Tadiwa On

I just figured it out.Go to: Tool>Build System>New Build... and paste this build.

{
    "cmd": ["g++.exe", "-std=c++23", "-o", "$file_base_name", "*.cpp", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
    "shell": true,
    "selector": "source.c++"    
}

Save it and go to Tools>Build With... Your file name should appear there and click on it. Whenever I want to run the program I just hit Tools>Build or more precisely I just use the keyboard shortcut. This build:

  • runs your program in an external window
  • allows you to interact with your program so it's for input as well not just output.
  • you can CREATE header files and implementation files quite nicely and they will compile together at the same time.

If you remove the portion of the build that reads: ".cpp", then you can't make an implementation file; only headers and the main.cpp(or the file with the "int main()" function). I hope this helps.