How to compile cpp and c files together in VS Code - c++

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

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}"
]

Related

No such file or directory while compiling c++ in vscode include onnxruntime api header file

I'm very new to c++. I use VSCode and Ubuntu 18.04. I'm trying to use onnxruntime api in c++ to deploy my net model file.
Firstly I just tested including the onnxruntime api header file
#include <iostream>
#include <ctime>
#include <vector>
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
using namespace std;
int main() {
auto start_time = clock();
cout << "hello world" << endl;
Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "test");
auto end_time = clock();
printf("Proceed exit after %.2f seconds\n", static_cast<float>(end_time - start_time) / CLOCKS_PER_SEC);
printf("Done!\n");
return 0;
}
And there's error in compiling my code. The output is
[test.cc 2023-01-05 10:08:28.381]
,,test.cc:4:10: fatal error: onnxruntime/core/session/onnxruntime_cxx_api.h: no such file or directory
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
My onnxruntime directory is '/home/jiahao/git/onnxruntime/include/' and I have added it in tasks.json and c_cpp_properties.json.
Here is my c_cpp_properties.json
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**",
"/home/jiahao/git/onnxruntime/include/",
"/home/jiahao/git/onnxruntime/include/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
And here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-I", "/home/jiahao/git/onnxruntime/include/",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
}
]
}
When I Ctrl+click the include line in my code, I will be directed to the correct onnxruntime_cxx_api.h. So I thought the include path is right but I don't know why I cann't compile my code.
I have solved this question. I downloaded the release version of onnxruntime. And in the release package I found header files and .so file. I added the include path in c_cpp_properties.json like this:
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**",
"/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/include/",
"/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/include/**",
"/usr/include/eigen3/"
],
"browse": {
"path": [
"${workspaceRoot}",
"/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/lib/",
"/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/lib/**"
]
},
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
And I added the include path, .so file path and lib name in tasks.json. Here I also used eigen3 library
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-I", "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/include",
"-I", "/usr/include/eigen3",
"-L", "/home/jiahao/lib/onnxruntime-linux-x64-1.8.0/lib",
"-l", "onnxruntime",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
}
]
}
Now I can compile and run my code correctly.

How to fix "undefined symbols for architecture arm64"

I'm running VS Code on MacOS, and I'm using clang to compile a simple "Hello World!" program in C++. However, when I try to run my program, VS Code gives me the following error message: Undefined symbols for architecture arm64: followed by dozens of references to the std library. At the bottom of the terminal, it says:
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Build finished with error(s).
The terminal process terminated with exit code: -1.
(a) What does this mean? and (b) how can I fix it?
HelloWorld.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "clang - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/HelloWorld.cpp",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "C/C++: clang build active file"
}
]
}
Thanks!
If you have multiple CPP files in the project then you need to add "${fileDirname}/*.cpp"
tasks.json
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${fileDirname}/*.cpp",
// "${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
If you're having a vector; error from vscode also add the c_cpp_properties.json file in .vscode directory :
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}

Installing 'Boost' library - using VSCode. Windows 10

I've been trying for several days to add a Path on VSCode to the 'boost' library I've installed for a practice I've got. I can't link up the code implementation with the library downloaded. It keeps printing on the Output: fatal error: boost/signals2/signal.hpp: No such file or directory. This is the c_cpp_properties.json I have:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Program Files/boost/boost_1_66_0"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
In which I specify the route to 'boost' in 'includePath'. This is my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe compilar archivo activo",
"command": "C:\\w64devkit\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lboost_system"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Tarea generada por el depurador."
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe compilar archivo activo ver(1)",
"command": "C:\\w64devkit\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lboost_system"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Tarea generada por el depurador."
}
],
"version": "2.0.0"
}
And this is my code implementation:
#include <boost/signals2/signal.hpp>
#include <iostream>
using namespace std;
int main() {
cout << "Hello world" << endl;
return 0;
}
Very simple, but the problem is the linking from the compiler to the library. I don't know what to do

VS Code: Include file not found in browse.path, but the IDE recognizes the name as I type it

I get Include file not found in browse.path.
However the IDE sees the path as I am typing #include "ntrip_caster.h".
It gave the header in a list but than a couple seconds later shows it can't find it once it is in the code. How to direct VS to where the headers are ???
Also note this is OS Ubuntu on WSL... I don't know if there is problems with paths with WSL Ubuntu.
I did the following: none worked...
go to File -> Preferences -> Settings in VS Code and change "C_Cpp.intelliSenseEngine": "Default" to "C_Cpp.intelliSenseEngine": "Tag Parser".
//#include "ntrip_caster.h"
//#include "/home/xxx/ntrip/src/ntrip_caster.h"
//#include "../src/ntrip_caster.h"
//#include "ntrip_caster.h"
//#include "ntrip_caster.h"
//#include "${HOME}/xxx/ntrip/src/ntrip_caster.h"
#include "ntrip_caster.h"
{
"configurations": [
{
"name": "Linux",
"includePath": [
//"${workspaceFolder}/**",
"/home/xxx/ntrip/src/"
//"${workspaceFolder}/nn"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
//"cStandard": "gnu17",
"cStandard": "c11",
//"cppStandard": "gnu++14",
"cppStandard": "c++17",
//"intelliSenseMode": "linux-gcc-x64"
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
//"-I./include -std=c++11 -pthread lpthread",
"-std=c++11 -pthread lpthread",
//"${file}",
//"/home/xxx/ntrip/src/mount_point.h",
"/home/xxx/ntrip/src/ntrip_caster.cc",
//"/home/xxx/ntrip/src/ntrip_caster.h",
"/home/xxx/ntrip/src/ntrip_util.cc",
//"/home/xxx/ntrip/src/ntrip_util.h",
"/home/xxx/ntrip/examples/ntrip_caster.cc",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Compile C++ Helloworld.cpp on VSCode and Mingw

I am trying to compile and run a simple HelloWorld program via VSCode on Windows 10 but it fails to compile, even when following their tutorial.
The only feedback I receive is:
The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin\g++.exe' -g c:\Users\admin\programming\helloworld.cpp -o c:\Users\admin\programming\helloworld.exe" terminated with exit code: 1.
I don't even receive any further information what exactly has failed. How can this can be fixed? Quite curiously, the same command
& 'C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin\g++.exe' -g c:\Users\admin\programming\helloworld.cpp -o c:\Users\admin\programming\helloworld.exe
performs perfectly well when I invoke it in a separate powershell.
C:\Users\admin\programming\Helloworld.cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
C:\Users\admin\programming.vscode\tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
C:\Users\admin\programming.vscode\c_cpp_properties.json
{
"configurations": [
{
"name": "GCC",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
"cStandard": "c11",
"intelliSenseMode": "gcc-x64",
"cppStandard": "c++17"
}
],
"version": 4
}
Can you try returning zero in your program?
return 0;
Maybe the program is actually compiling and running but the non-zero return is interpreted as a failure.