1

I just started using VSCode on a new C++ project and ran into a silly issue. Yes, I'm fully familiar with the problem described in long long int vs. long int vs. int64_t in C++, but this question is VSCode specific. The problem is simple: VSCode thinks int64_t is an alias for long long, while the compiler thinks it's an alias for long. This means, that the following code is correct according to the compiler, but not according to VSCode: int64_t x = 20; auto y = std::max(0L, x); For VSCode, 0L should be 0LL (but then it's the compiler that complains and refuses to compile the code).

How can I make VSCode agree with the compiler? I'm running VSCode on Mac but compiling code for Linux ARM64 using GCC. I'm guessing VSCode is pulling in the wrong C++ header files or something, but not sure how to point it to the right ones.

Lajos Nagy
  • 9,075
  • 11
  • 44
  • 55
  • 1
    My expectation is you need to edit your `c_cpp_properties.json` – drescherjm May 10 '23 at 14:40
  • The C++ standard has the following rule: **`long` is 32 bits, while `long long` is 64 bits.** At the same time, `int64_t` is just a type alias for `long long`. You are also not using GCC – this might sound confusing, but MacOS has a `gcc` command, but it's actually the CLang compiler, which is different than GCC for Linux. No matter what you select as the compiler, it's actually using CLang, and not GCC. – Viraj Shah May 10 '23 at 14:40
  • 8
    *The C++ standard has the following rule: **long is 32 bits, while long long is 64 bits*** - this is false. The standard doesn't have such a rule. – 273K May 10 '23 at 14:53
  • 2
    *int64_t is just a type alias for long long* -- No. What `int64_t` **must** be is a 64-bit signed integer type, by whatever means done by the compiler to fulfill that requirement. – PaulMcKenzie May 10 '23 at 15:05
  • 3
    @VirajShah -- that's not the rule. The rule is that `long` is **at least** 32 bits and that `long long` is **at least** 64 bits. There's no reason they can't both be 64 bits. `std::int64_t` is an alias for a 64 bit integer type. It doesn't have to be `long long`; it can be `long` or even `int` or `short` if that type is 64 bits wide. – Pete Becker May 10 '23 at 15:53
  • Maybe this is all you need: [https://gist.github.com/kbumsik/52ce3f41a62f2485c3da1a585674e550?permalink_comment_id=3055090#gistcomment-3055090](https://gist.github.com/kbumsik/52ce3f41a62f2485c3da1a585674e550?permalink_comment_id=3055090#gistcomment-3055090) – drescherjm May 10 '23 at 16:01

1 Answers1

0

Assuming you use Microsoft's C/C++ extension pack (ms-vscode.cpptools-extension-pack) you are probably facing mismatch of your intelli-sense settings and actual compiler settings. If you don't use CMake for your project, you need to specify the former explicitly in the .vscode/c_cpp_properties.json file (located in your project folder by default). Just add a separate configuration to the configurations array which points to the correct compiler and uses consistent mode:

{
    "configurations": [
        {
            "name": "GCC",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "linux-gcc-x64"
        },
        ..other configurations go here..
    ],
    "version": 4
}

Be advised that your actual path and configuration settings may differ, so you may want to play around with it until you find the best fit.

When the new configuration is ready, chose it from the bottom right corner of the VSCode screen: enter image description here

That should be it.

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49