C programming on PI using VS-Code. Get "undefined reference to '...' " when using the quote form of the include directive - visual-studio-code

I would like to program a PI via VS-Code. When I translate the program I get "undefined referenc to `...' ". It seems that the header files that I include via #include "..." are not translated. If I translate the data in the terminal and then link the headers afterwards, it works. If I have understood correctly, you can set this via the tasks.json. However, I have not yet found out how.
I would like to modify an existing C program that has already been programmed on a PI. The programming is to be done in VS-Code via SSH. However, as soon as I try to run the program, I get numerous error messages with "undefined reference to '...' ".
Even with the minimal program helloworld I get this notification as soon as I want to include local .h-files with #include "...". System files with #include <..> are found.
The following programme
#include <stdio.h>
int main()
{
printf("Hello World\n");
}
is translated without problems.
However, as soon as I add Messages.h
#ifndef MESSAGES_H_INCLUDED
#define MESSAGES_H_INCLUDED
void printMsg(char *str);
#endif // MESSAGES_H_INCLUDED
and Messages.c
#include "Messages.h"
void printMsg(char *str)
{
printf("Testausgabe: %s\n",str);
}
to the project and to the program
#include <stdio.h>
#include "Messages.h"
int main()
{
printf("Hello World\n");
printMsg("Somthing else.\n");
}
The compiler returns
/usr/bin/ld: /tmp/ccBVuwoo.o: in function `main':
helloworld.c:(.text+0x14): undefined reference to `printMsg'
collect2: error: ld returned 1 exit status
And this although all files are stored in the same folder.
If I create the files manually using the terminal and then link them, it works.
$ gcc -c helloworld.c
$ gcc -c Messages.h
$ gcc -o Hello helloworld.o Messages.o
Does this mean that I have to manually create a make file that I have to expand for each new header (that would be a bummer)? Couldn't this also be done automatically via VS-Code? Have I perhaps overlooked something here? Sorry, but I am very new to this.
I am grateful for any help.
Attached is also the automatically generated tasks.json from VSC.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc Aktive Datei kompilieren",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Vom Debugger generierte Aufgabe."
}
],
"version": "2.0.0"
}

Related

vscode+Arduino environment: vscode is not able to load system headers despite the configuration of paths

I'm really getting frustrated with this issue.
I have installed vscode(version: 1.69.2) & Arduino IDE(version 1.8.20) on my MacOS(version 12.5), I installed the Arduino extension in the vscode, and tried to get it work.
It just works fine if I don't include the system libraries, however if I tried to include a header file, example, the 'iostream', the complier prompts me as below:
Alternatives for iostream: []
ResolveLibrary(iostream)
-> candidates: []
main:1:10: fatal error: iostream: No such file or directory
#include <iostream>
^~~~~~~~~~
compilation terminated.
exit status 1
It looks like the VScode IntelliSense could find this file, if I click this file with Command key pressed, it will show me the location of it.
iostream can be found by vscode IntelliSense
I'm assuming that IntelliSense works with the paths I set, but the compiler just cannot, I tried editing the json file several hours last night but still no luck.
Anyone has any idea about this? Your help is greatly appreciated!
Here's part of my c_cpp_properties.json file:
{
"version": 4,
"configurations": [
{
"name": "Arduino",
"compilerPath": "/Users/steveyang/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++",
"compilerArgs": [
"-w",
"-std=gnu++11",
"-fpermissive",
"-fno-exceptions",
"-ffunction-sections",
"-fdata-sections",
"-fno-threadsafe-statics",
"-Wno-error=narrowing"
],
"intelliSenseMode": "gcc-x64",
"includePath": [
"${workspaceFolder}",
"/Users/steveyang/Library/Arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino",
"/Users/steveyang/Library/Arduino15/packages/arduino/hardware/avr/1.8.5/variants/standard",
"/Users/steveyang/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/lib/gcc/avr/7.3.0/include",
"/Users/steveyang/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/lib/gcc/avr/7.3.0/include-fixed",
"/Users/steveyang/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/avr/include"
],
"forcedInclude": [
"/Users/steveyang/Library/Arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino/Arduino.h"
],
"cStandard": "c11",
"cppStandard": "c++11",
"defines": [....
],
"browse": {
"path":[
"${workspaceFolder}/",
"/usr/local/include",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.1.6/include",
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include",
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"
]
}
}
]
}

Ncurses.h ( (.text+0xd): undefined reference to `initscr') visual code Ubuntu

I need to know why VS code is not compiling ncurses.h. but it is possible to compile in the terminal ubuntu. this message is appearing on the VS code. (.text+0xd): undefined reference to `initscr'
the code runner extension configuration add -lncurses before -o
"code-runner.executorMap": {
"c": "cd $dir && gcc $fileName -lncurses -o $fileNameWithoutExt && $dir$fileNameWithoutExt "
}
I solved this problem adding "-lncurses" in the "args" (tasks.json)
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-lncurses" <--------- add the library ("-lncurses" ) here
],

VScode wont detect the Arduino library

I am currently trying to control a servo from an Arduino for a laser turret through vscode. the problem is that vscode does not pick up on my library folder called <servo.h>. I have a folder named "workspace" with the servo library, the .ino file, and the workspace file for vscode.
Instead of platformIO, I'm using the Arduino extension for vscode. I'm using an Arduino UNO. here is the code so far:
#include <Servo.h>
Servo servo1;
Servo servo2;
int potval
int potpin = 0
void setup()
and here is what my c_cpp_properties.json file looks like:
{
"configurations": [{
"name": "Win32",
"includePath": [
"${workspaceFolder}
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}],
"version": 4
}
And I'm running Windows 10
Any local library file that is included must use "" instead of <>.
#include <Servo.h>
should be replaced with
#include "Servo.h"
Here is a post describing the difference between the two:
What is the difference between #include < filename > and #include “filename”?

How can I grab the Swift REPL compiler errors in JSON format

I'm looking to grab Swift compiler errors from the Swift REPL in a readable format such as JSON or XML
The best I can do is read it in as a String via standard in/out pipes. I have tried nearly every argument/option with swiftc with no luck.
I read that I can write Python scripts that extend LLDB but I wouldn't know where to start.
Currently the format reads in as:
/Users/joeblow/Desktop/pre-compiled.swift:1:38: error: cannot convert value of type 'Int' to specified type 'String'
var age = 10; var name: String = 12;
^~
But I'm interested moreso in something like this:
{
path: "/Users/joeblow/Desktop/compiled.swift",
line: 1,
position: 38,
error: "cannot convert value of type 'Int' to specified type 'String'"
}
I do not want to use Regex/String manipulation.
The swift compiler accepts the -parseable-output flag, which produces a structured output including the errors. For instance, if wrong.swift has the contents of your example, I see:
> swiftc -g -Onone -parseable-output wrong.swift
1149
{
"kind": "began",
"name": "compile",
"command": "\/Applications\/Xcode.app\/Contents\/Developer\/Toolchains\/XcodeDefault.xctoolchain\/usr\/bin\/swift -frontend -c -primary-file wrong.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk \/Applications\/Xcode.app\/Contents\/Developer\/Platforms\/MacOSX.platform\/Developer\/SDKs\/MacOSX10.13.sdk -g -emit-module-doc-path \/var\/folders\/zc\/dp8ptx_10pg8m6pnwpbnjms00000gp\/T\/wrong-bc5f07.swiftdoc -color-diagnostics -Onone -module-name wrong -emit-module-path \/var\/folders\/zc\/dp8ptx_10pg8m6pnwpbnjms00000gp\/T\/wrong-bc5f07.swiftmodule -o \/var\/folders\/zc\/dp8ptx_10pg8m6pnwpbnjms00000gp\/T\/wrong-bc5f07.o",
"inputs": [
"wrong.swift"
],
"outputs": [
{
"type": "object",
"path": "\/var\/folders\/zc\/dp8ptx_10pg8m6pnwpbnjms00000gp\/T\/wrong-bc5f07.o"
},
{
"type": "swiftmodule",
"path": "\/var\/folders\/zc\/dp8ptx_10pg8m6pnwpbnjms00000gp\/T\/wrong-bc5f07.swiftmodule"
},
{
"type": "swiftdoc",
"path": "\/var\/folders\/zc\/dp8ptx_10pg8m6pnwpbnjms00000gp\/T\/wrong-bc5f07.swiftdoc"
}
],
"pid": 33440
}
306
{
"kind": "finished",
"name": "compile",
"pid": 33440,
"output": "\u001B[1mwrong.swift:1:21: \u001B[0;1;31merror: \u001B[0m\u001B[1mcannot convert value of type 'Int' to specified type 'String'\n\u001B[0mvar name : String = 12\n\u001B[0;1;32m ^~\n\u001B[0m",
"exit-status": 1
}
However, while the REPL uses a swift compiler instance for parsing your expressions, it doesn't have a way to accept and pass "extra flags" to that compiler, so at present I don't think there's any way to get the REPL to emit errors in this form.
If you're interested in this capability then please file an enhancement request with the swift.org JIRA. If you are adventurous, you could even try adding it yourself. You would have to get the swift driver to pass this option to lldb, and then get lldb to parse and pass it to its compiler. Probably also have to extract only the errors from the output or it would be too noisy.

how to pass multiple args to vscode task command?

in tasks.json I am using the "args" property to specify the arguments to pass to "command":"gulp". But when I run the task in vscode, only the first argument is being passed to gulp.
I want to run a gulp task against a single file. In gulpfile.js I am using the process.argv array to retrieve the command line arguments. So, on the command line I enter "gulp copy3 --file abc.js" and the copy3 task is run. The code then reads the argv array to get the name of the file being copied.
this code works from the command line. But does not work when I run it as a task in vscode. How to do that?
the gulpfile.js code:
gulp.task('copy3', function( )
{
console.log(process.argv) ;
let pattern = '*.js' ;
// single file to copy
if (( process.argv.length >= 5 ) && ( process.argv[3] == '--file' ))
{
let fileName = process.argv[4] ;
pattern = fileName ;
}
console.log('pattern:' + pattern ) ;
return gulp.src(pattern).pipe(gulp.dest('dev'));
}) ;
the tasks.json file
{
"version": "2.0.0",
"tasks": [
{
"taskName": "copy3",
"command": "gulp",
"args": [ "copy3", "--file", "${fileBasename}" ],
"problemMatcher": []
}
]
}
Here is the terminal output:
[10:52:57] Using gulpfile C:\vscTest\rpgproj\gulpfile.js
[10:52:57] Starting 'copy3'...
[ 'C:\\Program Files\\nodejs\\node.exe',
'C:\\vscTest\\rpgproj\\node_modules\\gulp\\bin\\gulp.js',
'copy3' ]
pattern:*.js
[10:52:57] Finished 'copy3' after 16 ms
thanks,
I made a couple of small changes, try:
{
"label": "Tasks: copy3",
"type": "shell",
"command": "gulp",
"args": [ "copy3", "--file", "${fileBasename}" ],
"problemMatcher": []
}
and your entire code works perfectly. Make sure to reload vscode after modifying the tasks.json.
VSCode appears to have a built-in gulp extension. This seems to scan your gulpfile for tasks and list them for you. It also seems to ignore the args option.
The workaround is to use the full path to gulp as the command e.g. ./node_modules/.bin/gulp to bypass it.