user1538798
user1538798

Reputation: 1135

How to compile cpp and c files together in VS Code

I have a number of question for VS Code Settings

i am trying out to build a test.cpp file with cJSON.c and cJSON.h (from cJSON library) included in it. The code as below

#include <iostream>
#include <sstream>
#include "cJSON.h"

int main()
{
    std::cout << "ello world" <<std::endl;

    cJSON *fmt = NULL;
    cJSON* root = cJSON_CreateObject();
    cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack (\"Bee\") Nimble"));
    cJSON_AddItemToObject(root, "format", fmt = cJSON_CreateObject());
    cJSON_AddStringToObject(fmt, "type", "rect");
    cJSON_AddNumberToObject(fmt, "width", 1920);
    cJSON_AddNumberToObject(fmt, "height", 1080);
    cJSON_AddFalseToObject (fmt, "interlace");
    cJSON_AddNumberToObject(fmt, "frame rate", 24);

    char *tmp_json = cJSON_Print(root);


    std::stringstream myStreamString;
    myStreamString << tmp_json;
    std::string myString = myStreamString.str();
     std::cout << " json string is " << myString << std::endl;

     cJSON_Delete(root);
     free(tmp_json );

    return 0;
}

First, I have an error whenever I tried to rebuild test.cpp (ie I have successfully build it one time round)

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g /home/xx/test/* -o /home/xx/test/test
g++: fatal error: input file ‘/home/xx/test/test’ is the same as output file
compilation terminated.

I can solve only the problem by deleting the previous build or test and then rebuild

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

task.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${workspaceFolder}/*",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Wonder did I fail to set anything extra in the settings? Is this the way on how mixed c++/c development should be compiled?

Thanks

Regards

Upvotes: 0

Views: 328

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 39040

Using such an input "${workspaceFolder}/*" causes inclusion of the previously built program test to the compiler arguments.

Use two globs:

"args": [
  "-fdiagnostics-color=always",
  "-g",
  "${workspaceFolder}/*.c",
  "${workspaceFolder}/*.cpp",
  "-o",
  "${fileDirname}/${fileBasenameNoExtension}"
]

Upvotes: 1

Related Questions