DachuanZhao
DachuanZhao

Reputation: 1339

How to include other c++ header file in vscode?

My workspace is :

.
├── include
│   ├── simdjson.cpp
│   └── simdjson.h
├── src
│   ├── parse_json.cpp
│   └── twitter.json
└── third_party

My parse_json.cpp is

#include "simdjson.h"
int main(void)
{
    simdjson::dom::parser parser;
    simdjson::dom::element tweets = parser.load("twitter.json");
    std::cout << tweets << std::endl;
}

My c_cpp_properties.json is

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

It raises error when I press F5 in parse_json.cpp :

Starting build...
/usr/bin/g++ -g /mnt/c/Users/hnjyz/OneDrive/test/src/parse_json.cpp -o /mnt/c/Users/hnjyz/OneDrive/test/src/parse_json
/mnt/c/Users/hnjyz/OneDrive/test/src/parse_json.cpp:1:10: fatal error: simdjson.h: No such file or directory
    1 | #include "simdjson.h"
      |          ^~~~~~~~~~~~
compilation terminated.

My tasks.json is:

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

My launch.json is:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "console": "externalTerminal",
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

What should I do ?

Update one: When I change tasks.json to

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

It raise a different error :

/usr/bin/g++ -g /mnt/c/Users/hnjyz/OneDrive/test/src/parse_json.cpp -o /mnt/c/Users/hnjyz/OneDrive/test/src/parse_json -I/mnt/c/Users/hnjyz/OneDrive/test/include
/usr/bin/ld: /tmp/ccMAhnvY.o: in function `simdjson::haswell::(anonymous namespace)::numberparsing::parse_float_fallback(unsigned char const*, double*)':
/mnt/c/Users/hnjyz/OneDrive/test/include/simdjson.h:16770: undefined reference to `simdjson::internal::from_chars(char const*)'
/usr/bin/ld: /tmp/ccMAhnvY.o: in function `simdjson::westmere::(anonymous namespace)::numberparsing::parse_float_fallback(unsigned char const*, double*)':
/mnt/c/Users/hnjyz/OneDrive/test/include/simdjson.h:23215: undefined reference to `simdjson::internal::from_chars(char const*)'
/usr/bin/ld: /tmp/ccMAhnvY.o: in function `simdjson::fallback::(anonymous namespace)::numberparsing::parse_float_fallback(unsigned char const*, double*)':
/mnt/c/Users/hnjyz/OneDrive/test/include/simdjson.h:35813: undefined reference to `simdjson::internal::from_chars(char const*)'
/usr/bin/ld: /tmp/ccMAhnvY.o: in function `simdjson::error_message(simdjson::error_code)':
/mnt/c/Users/hnjyz/OneDrive/test/include/simdjson.h:6927: undefined reference to `simdjson::internal::error_codes'
/usr/bin/ld: /tmp/ccMAhnvY.o: in function `simdjson::dom::parser::allocate(unsigned long, unsigned long)':
/mnt/c/Users/hnjyz/OneDrive/test/include/simdjson.h:8140: undefined reference to `simdjson::active_implementation'
/usr/bin/ld: /tmp/ccMAhnvY.o: in function `simdjson::internal::mini_formatter::number(double)':
/mnt/c/Users/hnjyz/OneDrive/test/include/simdjson.h:8416: undefined reference to `simdjson::internal::to_chars(char*, char const*, double)'
collect2: error: ld returned 1 exit status

......

Upvotes: 0

Views: 3139

Answers (1)

Hanjoung Lee
Hanjoung Lee

Reputation: 2152

/usr/bin/g++ -g /mnt/c/Users/hnjyz/OneDrive/test/src/parse_json.cpp -o /mnt/c/Users/hnjyz/OneDrive/test/src/parse_json

In your compile command, there is no include directory argument.

Please add "-I${workspaceFolder}/include" to your tasks.json (to the "args" array). It will add your include directory to it.


ADDED - Regarding the linker error

It looks like simdjson is a single header and single source file library. And assuming you also have the source file(simdjson.cpp), you may add another argument "simdjson.cpp".

This would work, but not a very good way though. I recommend making a static/shared library for it in advance and give it to the linker to save your build time. Also I highly recommend doing this with CMake or other build management tools.

Upvotes: 2

Related Questions