Windows gui C build system in sublime text - c

I had linker error: undefined reference to windows.h functions like textout,selectobject,beginpaint. But I was able to remove the error
by adding "-mwindows" in sublime build system. But now im not able to get any output in windows terminal. Eg: if i try to use printf("test"); it won't show any output in windows terminal and textout() will only output textout string in gui window.
I tried it with codeblocks and it was able to get output of printf in windows terminal and textout in another window.
Is there anyway i can make sublime build system do the same?
{
"cmd": ["gcc", "-Wall","-mwindows","-pedantic-errors", "$file_name","-o", "${file_base_name}.exe", "&&", "start", "cmd", "/k" , "$file_base_name"],
"selector": "source.c",
"working_dir": "${file_path}",
"shell": true
}

If you want to build a console application, then you need to also add the -mconsole option to your compiler command line.
That will give you a Windows application that automatically creates and runs in the context of a console (text-based environment).
If you just pass -mwindows, then you get a standard Windows application that expects to have a GUI. You (your code) is responsible for creating a window and outputting text/graphics to that window. It doesn't happen automatically.

Related

Some problems when trying to implement C code on Visual Studio Code

I recently started doing some programming with C/C++. I was using Dev-C++ and today
i changed to Visual Studio Code because later on i'll need to work in other programming languages too, like Python, R etc. I successfully installed it and i added the extensions of C/C++ and Code runner. Although am having some problems when i try to run the following two basic programs:
#include <stdio.h>
int main(void){
printf("hello world\n");
return 0;
}
and
#include <stdio.h>
int main(void){
int x;
printf("Give a number: ");
scanf("%d",&x);
printf("Your number is: %d\n",x);
return 0;
}
In the first program i get the following output in the integrated terminal of VS code:
My first question is a matter of appearance, how can i remove the path that shows above the "hello world" output? Also, notice that i am on the terminal option, if i switch to the output option, nothing is being displayed there, although i have seen in some tutorials that they can display their results in the output option which is more minimalistic than the terminal, in addition it displays the run time of the program which is a nice feature.
In the second program when i press the Run Code button it opens my cmd first and asking to give a number, after i give the number and press Enter i can type the asked number also in the integrated terminal of VS and get the result. Is there anyway to surpass my cmd? I.e. to run the program only in VS without running it also through the cmd? Note that i have already checked the option "Run In Terminal" in the settings of the code runner extension.
Thanks in advance!
So I believe you are on windows, simply installing the extension wont help, there are a few more steps to this.
You'll create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the g++ compiler to create an executable file based on the source code.
From the main menu, choose Terminal > Configure Default Build Task. In the dropdown, which will display a tasks dropdown listing various predefined build tasks for C++ compilers. Choose g++.exe build active file, which will build the file that is currently displayed (active) in the editor.
This will create a tasks.json file in a .vscode folder and open it in the editor.
Your new tasks.json file should look similar to the JSON below:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
To run the build task defined in tasks.json, press Ctrl+Shift+B or from the Terminal main menu choose Run Build Task.
You can run your program in the terminal by typing .exe (or .<your program name.exe if you use a PowerShell terminal).
Hello you should try to install the Code Runner extension. You will run your future C and Python code easily.

How to build and run C programs in sublime text 3 mac osX

I have installed sublime text 3 and a package control for it. Now I'm trying to build my hello-world.c but I cannot build it. When I open tools --> build with.. it offers me C++ single file but I want to build my program with C. There is no C alternative in the build systems menu.
First create your file :
/* hello.c */
#include <stdio.h>
int main() {
printf ("Hello world!\n");
return 0;
}
save it something like hallo.c then compile it with gcc. So open your terminal go in your directory fileand type gcc hallo.c -o hallo and run ./hallo
To buit it on sublime plese do this :
In Sublime, click Tools -> Build System -> New Build System...
{
"cmd" : ["gcc", "-o", "$file_base_name", "$file_name"],
"cmd" : ["./$file_base_name"],
"selector" : "source.c",
"shell" : false,
"working_dir" : "$file_path"
}
Or here
Goto tools > Change build system
Anyway, I suggest you use an IDE or much better use just file & compilator.
Sublime text is just and IDE.
Install a C compiler and build and run using that compiler.
Try some sublime plugin compiler.

First steps in Eclipse

I try to figure out Eclipse by writting a HelloWorld.c from scratch and hope that you can give me some help concerning the problems I face.
When launching Eclipse, I select the standard workspace.
In the Project Explorer, I create a new C project, called HelloWorld. Settings are "Empty Project" and "MinGW". Next: Debug and Release activated. Finish.
In the Menu toolbar "Window->Preferences", I make sure that under "General->Workspace" the "Text File Encoding" is set to UTF-8 and "New Text File Line Delimiter" to Unix. Apply and OK. Again under "Window->Preferences->C/C++->New C/C++ Project Wizard", under "Toolchains" I select "MinGW GCC". "Make Toolchain(s) preferred", "Apply", and "OK".
Now all the necessary setings for this C project are saved in a settings file in the project folder - if I understand this right.
In the Project Explorer, ther HelloWorld Project contains now a header-folder with three subfolders (C:/MinGW64/lib/gcc/x86_64-w64-mingw32/someversion/include and .../include-fixed, and C:/MinGW64/x86_64-w64-mingw32/include).
I create a new C Source File with explicit name "HelloWorld.c" and I get a nice window with lines to write any desired code and also an outcommented "HelloWorld.c"-title on top.
So I write
#include <stdio.h>
main(){
printf("Hello World!");
return 0;
}
and save it. So far, so good.
Now, I press "Build All" and get a Warning "return type defaults to 'int' [-Wreturn-type]".
When I press "Debug", I get the message "Info:Nothing to build for HelloWorld" but when I press "Run", the Console shows "Hello World!" anyway.
In previous (very same) projects, I already got error messages like "Launch failed. Binary not found." which disappeard after many times pressing "Build All" and "Run". And also the error message "Program file does not exist." although I just wrote and saved the above C Source File code. I could not solve it, deleted the Project, restarted Eclipse and did all again as explained above.
What did I or am I doing wrong?

mono - winforms not working properly with f#

I have successfully installed MonoDevelop with the F# bindings (under Linux Arch).
If I try a simple HelloWorld with winforms, I get this strange behavior: the program compiles and builds successfully, but a moment after the window is loaded it just closes itself and the program returns with no error messages.
I checked the logs and it seems no error is thrown (except from the one discussed here: GLib-CRITICAL **: Source ID XXX was not found when attempting to remove it, which anyway seems unrelated).
The code for the program:
open System.Drawing
open System.Windows.Forms
[<EntryPoint>]
let main argv =
let form = new Form(TopMost = true, Text = "Hello World")
form.Show()
0 // return an integer exit code
If I try to run it through the interactive console, the window is sort of freezed: I can move it around, but I can't close it (have to shut down monodevelop in order to do so).
I also tried to compile it via terminal, using:
fsharpc Program.fs -pkg:dotnet.dll
mono Program.exe
but it behaves the same way as if I was running through MonoDevelop (window opens then shuts down).
Other than that F# seems to work fine, I can run console programs without any problem.
I also tried to run a simple HelloWorld program with C# in MonoDevelop, and it just works fine.
Any tips?
You need to start the forms event loop with something like
Application.Run(new Form())

How to compile and run C code in GVim on Windows?

I'm using GVim on Windows 7 and trying to compile and run my code right in Vim(just like in visual studio).
I've installed minGW, and correctly set the Environment Variable of Windows.
Look at the image below, when i click the shovel, whose function is to "make current project", error message appear(in lower part of screen).
How to fix this~~ thanks^^
Try vim-quickrun plugin.
Write C++ code in the buffer (it should have cpp filetype), and try
:QuickRun
I use vim-quickrun always. I add following in my .vimrc.
silent! nnoremap <unique> <silent> <leader>r :<c-u>QuickRun<cr>

Resources