The recommended Microsoft C/C++ extension for VSCode works great for C++ but fails at some places with C giving false errors. Here's one I have found:
void f(int a,int b, int arr[][b]);
This is valid C code and yet it says there's an error.
So how do I properly set up my intellisense to account for such nuances?
Related
I have a dart method that invokes some C code using dart:ffi. When the C code fails (for example due to some memory violation), I don't get any error message to indicate what's wrong, but only the whole process exits immediately with these messages:
[+41627 ms] Service protocol connection closed.
[ ] Lost connection to device.
[ +1 ms] DevFS: Deleting filesystem on the device (file:...)
I am not asking about catching the error, as I know these kinds of errors are fatal, I just want some useful logs to see whats happening.
Can I debug the native code somehow or at least get useful error messages about the reason/location of the crash?
Assumptions: debugging a Windows DLL on Windows using a pure Dart test harness. It should work pretty much the same for a Flutter test harness (i.e. the example app in a Flutter plugin project), but in all likelihood you'll want to do much of the testing in as simple a test environment as you can.
You are going to need two projects: a Visual Studio DLL project and a pure Dart command line. (In what follows my examples are in d:\vcc\Dll2\ and d:\source\testffi2\ respectively. The Dart project has a main function in bin\testffi2.dart.)
For testing I removed the VS C++ DLL project boilerplate and replaced it with:
#include "pch.h"
#include "framework.h"
#include "Dll2.h"
#include <cstdint>
extern "C" __declspec(dllexport) int32_t sum(int32_t a, int32_t b) {
return a + b;
}
and checked that it compiled OK (in Debug/x64 configuration). This built: D:\vcc\Dll2\x64\Debug\Dll2.dll as expected.
In the Dart project I created a simple test harness to load the DLL and the function and call it.
import 'dart:ffi';
import 'dart:io';
void main() {
final dll = DynamicLibrary.open('D:\\vcc\\Dll2\\x64\\Debug\\Dll2.dll');
final sumFunction = dll.lookupFunction<
Int32 Function(
Int32 a,
Int32 b,
),
int Function(
int a,
int b,
)>('sum');
print(sumFunction(123, 321));
sleep(Duration(seconds: 5)); // this is just there so that the VCC debug
// window doesn't immediately close
}
Running this is the Dart IDE or from the command line does, indeed, print 444 as expected, but doesn't give you an opportunity to debug the C.
Finally, back in the Visual Studio project, change the debugging properties to launch the Dart test harness. Set the exe path to the Dart sdk binary, the command line to the Dart main file, and the working folder to the Dart project folder.
Finally, set a breakpoint on the return a + b line and click the Local Windows Debugger button. Execution pauses as expected in the Visual Studio debugger and the values of local variables a and b are visible, etc.
I edit my C code with VS Code (1.17.0) with C/C++ Extension (1.12.0) that provides error checking. I compile my code with GCC compiler that supports nested functions. So if I write this
int foo(int x)
{
int bar(int x)
{
return x * 2;
}
return bar(x) + 1;
}
and compile it with gcc it works fine. However, the extension doesn't think so and red-squiggles the second curly brace with a "semicolon expected" error. The extension allows you to choose IntelliSense mode which I set to "windows-gcc-x64", however it doesn't seem to work. This issue exists since 2017. Is there a fix or a workaround?
C/C++ extension for VSCode does not support nested functions yet.
There is an issue open in their repo already, which you can track here.
... it's been there since 2017 though
I am using Vsc to write my program in C, the program should return an error but nothing is shown, only the final code is provided (code = 3221225477), how can I get the error displayed?
The final output is: [Done] exited with code=3221225477 in 0.569 seconds
#include <stdio.h>
#include <stdlib.h>
int main(){
char x = 'a';
printf("%s", x); //Error cause i'm using %s for a char
}
P.s. I have already installed .Run and the C / C ++ extension
The Microsoft C/C++ extension if configured properly (problemMatcher in tasks.json) should also trigger similar errors based on the compiler you select.
After investigation, you might get some help from the clangd extension.
Install the extension in VS Code from https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
Open your project folder in VS Code and open the source file (like main.c).
Then this extension should run clangd in the background and show a warning "Format specifies type 'char *' but the argument has type 'char' (fix available)".
I am using Code::Blocks 13.12 for programming in C. After building and running my simple HelloWorld.c program, it gives an error.
Error: An alert box pop up saying -"cc1.exe has stopped working.A problem caused the program to stop working correctly.Windows will close the program and notify you if a solution is available."
I tried using Notepad++ , the same pop up appears saying a.exe has stopped working.
I am a naive,so have no idea on how to deal with this.
My queries:
1) Am I using an outdated version? If not, how can I get rid of this problem?
2) Which is the most efficient IDE available for C/C++ if Code::Blocks is not that efficient?
My requirement"
-I had been using Turbo C and it doesn't give errors like segmentation fault and other memory related errors that we get in online Compilers or compilers of competitive programming.So, I need an efficient compiler which behaves same as online compilers, so I don't get stuck while solving problems during competitive programming.
This is the simple code giving error:
#include <stdio.h>
void main()
{
printf("Hello world!");
//return 0;
}
Edit:
Even after changing the code to return int, it's giving an error.I tried editing the code as below, but the same window is being popped up again.
int main(void)
{
printf("Hello world!");
return 0;
}
There's no problem with your code, though indeed, as pointed out in the comments, neither void main() nor int main(void) is considered correct, but that's not what's causing the problem. cc1.exe is a MinGW-related file (MinGW is the GCC port for Windows that Code::Blocks uses by default for compilation); if it's crashing, it's possible the installation is corrupt.
I suggest you try reinstalling MinGW - remove Code::Blocks, install the standalone MinGW version, then download Code::Blocks without the compiler suite, install it and configure to use your version of MinGW.
For me, the solution was to choose,
Select target -> Release
from the Build menu. It took lots of time for me to find this!
How can I create a basic C app in Visual Studio, be it 2010 Ultimate or 2008 Professional? I have searched through the project templates, and can find plenty for C++, but none for C.
(I'm hoping that the compiler and debugger will be built in.)
New project/Win32 Console Application/Empty project.
Add a file called "hello.c" (important that it's .c)
Type out a basic hello-world:
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
Compile, execute... PROFIT!
Visual Studio doesn't have a separate compiler for C, it uses the C++ compiler for C code. You can tell it to restrict itself to legal C syntax by using a compiler switch or by renaming the .cpp file to .c
Edit: I'm still using 2005 on this machine, so it might not be in the same place, but try this
Right click on the main.cpp in the solution explorer pane (on the right).
Choose Properties (bottom of the menu)
Open up the C/C++ group of properties
choose Advanced page
Change "Compile As" property to "Compile as C Code (/TC)"
In addition to changing the file extension, you may need to change the compiler settings:
Settintgs of Application->C/C++->addition->compiler such...
You must take /TC for basic C and /TP for C++.
https://msdn.microsoft.com/ru-ru/library/032xwy55.aspx
Good Luck.