Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

c++ - How to use visual studio code to compile multi-cpp file?

I have followed some instructions to construct Visual studio code C/C++ compile and debug environment.But g++ compiler can only compile the selected cpp file, so the included .h file associated the cpp file can not compiled. then the terminal shows 'Undefined symbols for architecture x86_64' error. the code as below:

the a.h file

    int func();

the a.cpp file

    include <iostream>
    include "a.h"
    using namespace std;
    int func(){
        return 111;
    }

the main.cpp file

    include "a.h"
    using namespace std;
    int main()
    {
        int b = func();
        cout << b << endl;
    }

Visual studio code will use the command as below

     g++ directory/main.cpp -o directory/main.out -g -Wall -fcolor-        diagnostics -std=c++11

this command will raise 'Undefined symbols for architecture x86_64' error I can fix it with this new command

    g++ main.cpp a.cpp -o main.out.

So the problem is how to config these json files to fix the g++ compile issue. And when I want to use some libraries such as FFMpeg, how can I link the FFMpeg .h file correctly.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way I have gotten it to work is by going into your build task, and instead of saying "g++ ${file}", instead you can set the target file to get compiled as "g++ ${fileDirname}/**.cpp" which will compile all the .cpp files in the directory.

This way you can use the same build task for a project where you may have multiple programs in different folders, all under the same umbrella directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...