1

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

MattDMo
  • 100,794
  • 21
  • 241
  • 231

2 Answers2

0

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.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • First of all, thanks for your help, Where would I place the makefile, in the same folder as the main.cpp? Also I know there is such a thing as makefile on unix based systems, but is that the same for windows? I'm running W10 on this machine – Peter Williamson Sep 15 '20 at 17:25
  • You can put the Makefile wherever you want, just make sure the path(s) to the source files are specified within it. For a small project like this, just put it in the same directory as the source and header files. You can install [Cygwin](https://www.cygwin.com/) to get the full ecosystem of dev tools that are normally found on Unix/Linux, although if you're already using gcc, you probably have something like Cygwin/MSYS/MingW installed already, right? – MattDMo Sep 15 '20 at 17:32
  • I personally use Cygwin, which incorporates some components from MinGW and possibly MSYS as well (although I may be confusing it with Git Bash), all in a nice easily-configurable package. Also, one advantage (for some) to using CMake over Makefiles is the availability of a pretty decent GUI tool, although you still need to have a general idea of what you're doing. – MattDMo Sep 15 '20 at 17:36
  • @PeterWilliamson sorry, forgot to tag you in the other comments... – MattDMo Sep 15 '20 at 17:38
  • I've installed cywin for windows, How would I now for example make a buildfile for sublime text? And yes, I've got minGW installed – Peter Williamson Sep 15 '20 at 21:43
  • @PeterWilliamson create a `Makefile` either in your project's root directory or in the directory with all your source files, open one of those files, select **`Tools → Build System → Make`**, then hit `Ctrl-B` to build. – MattDMo Sep 16 '20 at 22:38
0

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.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Tadiwa
  • 1
  • 1