Build (and run) Pascal with Sublime Text - build

I wanted to build and run Pascal in Sublime Text 2 (or 3). I've searched as much as I could and although I could get sublime to BUILD my dpr or pas files in ST3, I struggle with getting them to run as an exe in command prompt. Therefore, I tried using this, as someone suggested (Qwerty answered this question on someone else's post):
{
"cmd": ["fpc", "${file_path}/${file_base_name}"],
"selector": "source.pascal",
"variants": [
{
"cmd": ["start", "cmd", "/c", "$file_base_name.exe & pause"],
"name": "Run",
"shell": true
}
]
}
It works perfectly fine when compiling and running hello world, but gives me errors with everything else:
Free Pascal Compiler version 2.6.4 [2014/03/06] for i386
Copyright (c) 1993-2014 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling F:\Ali\Pascal\Projects\adult.pas
Linking F:\Ali\Pascal\Projects\adult.exe
adult.pas(32,1) Error: Can't create object file: F:\Ali\Pascal\Projects\adult.exe
adult.pas(32,1) Fatal: Can't create executable F:\Ali\Pascal\Projects\adult.exe
Fatal: Compilation aborted
Error: C:\FPC\2.6.4\bin\i386-Win32\ppc386.exe returned an error exitcode (normal if you did not specify a source file to be compiled)
[Finished in 0.4s with exit code 1]
Whereas if it's Hello World:
Copyright (c) 1993-2014 by Florian Klaempfl and others
Target OS: Win32 for i386
Compiling F:\Ali\Pascal\Projects\hello.pas
Linking F:\Ali\Pascal\Projects\hello.exe
4 lines compiled, 0.3 sec , 25936 bytes code, 1644 bytes data
[Finished in 0.5s]

This build system compiles your program AND runs it automagically. Although if you just want to compile or you only want to run the program you can do it by pressing Ctrl-Shift-B.
{
"shell": true,
"cmd": ["fpc", "${file_path}/${file_base_name}", "&&", "start", "cmd", "/c", "$file_base_name.exe", "&", "pause"],
"selector": "source.pascal",
"variants": [
{
"cmd": ["start", "cmd", "/c", "$file_base_name.exe & pause"],
"name": "Run",
"shell": true
},
{
"cmd": ["fpc", "${file_path}/${file_base_name}"],
"name": "Compile",
"shell": true
}
]
}

Check your processes. The exe that was built before is still running and locked by OS so that the fpc can't overwrite it. Hello was probably not running, so it was ok.
Everything else worked here.
My variant:
{
"cmd": ["C:\\lazarus\\fpc\\2.6.4\\bin\\i386-win32\\fpc.exe", "$file"],
"selector": "source.pascal",
"variants": [{
"cmd": ["start", "cmd", "/c", "$file_base_name.exe & pause"],
"name": "Run",
"shell": true
}]
}
PS! I didn't know you could do that. I like Sublime now even more ! Thanks.

Environment: macOS Catalina, Sublime Text 3.
You should uninstall Free Pascal if you installed it with * .dmg
You can find detailed instruction here: https://wiki.freepascal.org/Uninstalling_Lazarus_on_macOS
Install Free Pascal via Brew:
$ brew install fpc
In Sublime create build system for Pascal language:
[Tools] -> [Build System] -> [New Build System...] and replace default code with code below:
{
"shell_cmd": "fpc \"${file}\" -o\"${file_path}/${file_base_name}\" && open -a Terminal.app \"${file_path}/${file_base_name}\"",
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.pascal",
"variants":
[
{
"name": "Compile Only",
"shell_cmd": "fpc \"${file}\" -o\"${file_path}/${file_base_name}\"",
"shell":true
},
]
}
When you press Cmd-B it automatically compiles AND runs your program. Shift-Cmd-B displays two options "Pascal" (compile & run) and "Pascal - Compile Only".
Answer is based on:
https://gist.github.com/dominik-hadl/6c24b3836c435dacb674

Related

Visual Studio Code C++ Build finished with error(s)

I'm trying to run a simple hello world application. Whenever I try to build using g++ it gives me the following error. I can't generate the .exe file or anything. I re-installed mingw using msys2. Walked through tutorials etc.. same problem occurs. The paths provided below are supposed to be correct made sure of it
* Executing task: C/C++: g++.exe build active file
Starting build...
C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall H:\VSCode\CPP\helloworld.cpp -o H:\VSCode\CPP\helloworld.exe
The system cannot find the path specified.
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
* Terminal will be reused by tasks, press any key to close it.
I walked through tutorial to build and debug c++ from the beginning to the end still the same problem. tasks.json code is below. I sticked with default "args" but same problem as well. I copied the tutorials args same problem.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g3",
"-Wall",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
// "${fileDirname}" also gives the same problem.
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
},
{
"type": "shell",
"label": "Run C/C++: g++.exe",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe -g3 -Wall \"${file}\" -o \"${fileDirname}\\${fileBasenameNoExtension}.exe\"",
"options": {
"cwd": "C:\\msys64\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"detail": "compiler: C:\\msys64\\mingw64\\bin\\g++.exe"
}
]
}
edit:
helloworld.cpp
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World!"<<endl;
return 0;
}
When I run it using Run Code using CodeRunner extension it runs normally, but Run/Debug C++ Code gives the error provided.
Whenever I try to see the errors, it shows nothing.
Another Edit:
when running C:\msys64\mingw64\bin\g++.exe -fdiagnostics-color=always -g3 -Wall H:\VSCode\CPP\helloworld.cpp -o H:\VSCode\CPP\helloworld.exe in cmd it build successfully and generate the .exe file.

Setup vscode to compile C files

Im trying to setup vscode to compile and run C files, but I am having problems with setting up the tasks.json file.
I guess what I'm really asking is how to include code from outside the main file. Im trying to include a file "stack.h" from a folder "include" but it's not working.
I get this error in vscode:
ld: can't link with a main executable file 'gcc' for architecture
x86_64 clang: error: linker command failed with exit code 1 (use -v to
see invocation) The terminal process terminated with exit code: 1
My tasks.json file looks like this
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"gcc",
"-o",
"stack_test",
"-I${fileDirname}/../include/",
"${file}",
"${fileDirname}/../src/stack/stack.c",
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
I can compile this file normally through the terminal with the following
gcc -o stack_test -I../include/ stack_test.c ../src/stack/stack.c
What am I missing here?
The problem could be that the compiler is not in the platform path. Visual Studio Code is therefore not able to find it.
See: https://code.visualstudio.com/docs/languages/cpp

How to compile/debug a C++ application in Docker with Visual Studio Code on Windows

I'm new on Visual Studio Code and Docker. Now I want to use Visual Studio Code to edit my C++ code and Docker to compile/debug.
I don't know how to write the launch.json and task.json correctly files, so that I can use Docker to compile/debug my C++ application just under Visual Studio Code development environment. Is there a solution for this problem?
Here is my platform information:
OS: Windows 10
Visual Studio Code: v1.25.1
OS in Docker: Ubuntu 16.04 (Xenial Xerus)
Compiler in Docker: g++
This answer assumes that you are not trying to do anything with multiple containers... I'm assuming that you just want to use a single container to build some C++ code, and that all of your code is in a folder called C:\vsc_docker_cc_gdb. I also assume you have the C++ and Docker extensions from Microsoft installed in Visual Studio Code.
Let's start with a simple C++ file, called hello.cc:
#include <iostream>
int main(int argc, char **argv) {
std::cout << "Hello from Docker" << std::endl;
}
Let's also add a Makefile:
CXXFLAGS = -O3 -ggdb -m64
LDFLAGS = -m64
all: hello.exe
.PRECIOUS: hello.exe hello.o
.PHONY: all clean
%.o: %.cc
$(CXX) -c $< -o $# $(CXXFLAGS)
%.exe: %.o
$(CXX) $^ -o $# $(LDFLAGS)
clean:
rm -f hello.o hello.exe
Here's a Dockerfile that extends gcc:latest by adding GDB and gdbserver (note: I'm not sure gdbserver is needed):
FROM gcc:latest
LABEL Name=vsc_docker_cc_gdb Version=0.0.2
RUN apt-get -y update
RUN apt-get -y install gdb gdbserver
WORKDIR /root
Here's .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build (in container)",
"type": "shell",
"command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root vsc_docker_cc_gdb make",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
{
"label": "clean (in container)",
"type": "shell",
"command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root vsc_docker_cc_gdb make clean",
"group": "build",
"problemMatcher": []
},
{
"label": "remove containers",
"type": "shell",
"command": "docker ps -a -q | % { docker rm $_ }",
"problemMatcher": []
},
{
"label": "run the code",
"type": "shell",
"command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root vsc_docker_cc_gdb ./hello.exe",
"group": "build",
"problemMatcher": []
},
{
"label": "prepare to debug",
"type": "shell",
"command": "docker run --privileged -v c:/vsc_docker_cc_gdb/:/root --name debug_vsc -it vsc_docker_cc_gdb ",
"group": "build",
"problemMatcher": []
}
]
}
And finally, .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [{
"name": "(gdb) Pipe Launch",
"type": "cppdbg",
"request": "launch",
"program": "/root/hello.exe",
"cwd": "/root",
"args": [],
"stopAtEntry": true,
"environment": [],
"externalConsole": true,
"pipeTransport": {
"debuggerPath": "/usr/bin/gdb",
"pipeProgram": "docker.exe",
"pipeArgs": ["exec", "-i", "debug_vsc", "sh", "-c"],
"pipeCwd": "${workspaceRoot}"
},
"MIMode": "gdb",
"setupCommands": [{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}]
}, ]
}
There are two important things here. The first is that you'll notice that parts of launch.json are referring to paths in the container (/root/) and others are referring to paths on the Windows host (workspaceRoot). That is important.
The second is that you'll need to have a container running, and then you can launch a debug process into it. Here's a recipe to go from zero to starting that special container and launching a debugger in it.
From PowerShell: docker pull gcc
From Visual Studio Code: F1, Docker: Build Image (pick vsc_docker_cc_gdb:latest)
From Visual Studio Code: Ctrl + Shift + B to build the code
From Visual Studio Code: F1, Tasks: Run Task (pick "remove containers")
From Visual Studio Code: F1, Tasks: Run Task (pick "prepare to debug")
From Visual Studio Code: F5 to start the debugger
From there, the Visual Studio Code Debug Console should work, and you should be able to set breakpoints, watch variables, and enter debug commands.
I set up a minimal working example on GitHub: https://github.com/fschwaiger/docker-cpp-vscode
The idea is as follows, assuming you have the ms-vscode.cpptools extension:
You need containers with gcc and gdb installed (can be the same)
You build the application in the container
You run gdb from within the container
1. Get the images for gcc and gdb
gcc is available directly from Docker Hub: docker pull gcc. I did not find gdb there, so there is a Dockerfile to build it:
FROM gcc:latest
RUN apt-get update && apt-get install -y gdb
RUN echo "set auto-load safe-path /" >> /root/.gdbinit
It builds on gcc:latest and installs gdb, so you can use the same image to compile and debug. It also sets option set auto-load safe-path / in /root/.gdbinit to suppress a warning when running gdb in the container. Safety should not be a concern for local development.
Build the image using docker build -t gdb . in the working directory, or in Visual Studio Code run the preconfigured task build docker gdb from F1 → Run Task.
2. Building the application
In the project, run docker run --rm -it -v ${pwd}:/work --workdir /work gcc make debug from a PowerShell window in the working directory. Using Visual Studio Code, this can be done by the preconfigured task make debug from F1 → Run Task.
3. Debug the application
You want to configure Visual Studio Code to run /usr/bin/gdb from within the container. You can use the pipeTransport option in launch.json for that and make it run:
docker run --rm --interactive --volume ${workspaceFolder}:/work --workdir /work --privileged gdb sh -c /usr/bin/gdb
Explanation:
--privileged: allow binary debugging
--volume ${workspaceFolder}:/work --workdir /work: mount the project folder into the container
--rm: remove the container after exit
--interactive: VSCode will issue interactive commands to the gdb shell
sh -c: defines a shell entrypoint within GDB is run
The overall launch.json looks like follows. Notice that program and cwd are the paths within the container. sourceFileMap allows the debugger to match the breakpoints with the source files. The rest is the default template stuff from the C++ extension.
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Docker",
"type": "cppdbg",
"request": "launch",
"program": "build/apps/program",
"args": [],
"stopAtEntry": true,
"cwd": "/work",
"environment": [],
"externalConsole": true,
"preLaunchTask": "make debug",
"targetArchitecture": "x64",
"sourceFileMap": { "/work": "${workspaceFolder}" },
"pipeTransport": {
"debuggerPath": "/usr/bin/gdb",
"pipeProgram": "docker.exe",
"pipeArgs": ["run","--rm","--interactive","--volume","${workspaceFolder}:/work","--workdir","/work","--privileged","gdb","sh","-c"],
"pipeCwd": ""
},
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
With this setup, all you need to do is press play in the debug workspace.

Visual studio code, compiling and running in single command

I have specified a pre-launch task to compile c++ code before launching in the launch.json file.
The build command is outlined in my tasks.json file:
"version": "0.1.0",
"command": "bash",
"isShellCommand": true,
"args": ["-c"],
"showOutput": "always",
"tasks": [
{
"taskName": "g++",
"isBuildCommand": true,
"args": [
"g++ /Users/user/OneDrive/Programming/TicTacToe/TicTacToe.cpp -o /Users/user/OneDrive/Programming/TicTacToe/a.out"
],
"showOutput": "always"
}
]
The output of this when I run this is that I receive a clang error:
clang: error: no input files
I'm not sure why this is happening as when I copy paste this bash command to terminal it works, but for some reason I am getting clang error from visual studio. Can anyone familiar with clang figure out what's wrong here?
The problem was that the string passed included the taskname for some reason.
It worked after I addded
"suppressTaskName": true
to tasks.json

g++ Fatal Error when using Sublime Text 2 to Build C++ Code

I am new to Sublime Text 2 and am trying to get it to build and run a simple C++ program but I am running into some errors.
I have the MinGW C++ compiler installed and set the path, however, I am still getting an error when I try to build it:
g++: error: : No such file or directory
g++: fatal error: no input files
I'm not sure what I should do in order to correct the problem, and info would be super helpful!
Thanks!
What's your .sublime-build file like?
I use this one and it works just fine.
{
"cmd": ["g++", "-Wall","-time","-g", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["${file_path}/${file_base_name}"]
}
]
}