0

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

1
  • 2
    This problem has nothing to do with mixing languages. "${workspaceFolder}/*" is a bad idea. For a very small C and/or C++ project, you may get away with something like "${workspaceFolder}/*.c*" or, better yet, "${workspaceFolder}/src/*.c*" (and move your build results to a separate directory, e.g. "${workspaceFolder}/build/", for sheer sanity). For anything larger, use e.g. a CMake extension. And never use the name test for your programs. test is a built-in command in Linux and Windows shells. Commented Oct 9, 2022 at 22:19

1 Answer 1

1

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}"
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.