简体   繁体   中英

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

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM