0

I am trying to compile a C program and display its output in a tab from Vim.

My set up looks like this:

hellomain.c

#include<stdio.h>
#include<stdbool.h>

int main()
{
    int i = 100;
    bool b =0;
    printf("hello world :)");
    return  0;
}

makefile

program: hellomain.c
       gcc -o hello hellomain.c

When I run :make | copen, courtesy of this post, I see a window like this:

gcc -o hello hellomain.c

Press ENTER or type command to continue

After pressing the enter, the program compiles and I see a new horizontally split tab containing the gcc command, but not the output of the program:

enter image description here

What's the issue here?

Cody
  • 2,480
  • 5
  • 31
  • 62
  • You only build, but never execute your program. – Daniel Heinrich Mar 26 '20 at 12:19
  • 1
    hello :)))))))) – mlwn Mar 26 '20 at 12:30
  • Ps those are windows not tabs – D. Ben Knoble Mar 26 '20 at 12:56
  • @DanielHeinrich how to do so? – Cody Mar 26 '20 at 13:33
  • @DanielHeinrich, without leaving the vim. I've tried `:make && ./hello | copen`, but it gives the wrong output. Here is the screenshot https://imgur.com/a/2MaCX2U – Cody Mar 26 '20 at 13:40
  • @DanielHeinrich From where the pipe character enters the scene? – Cody Mar 26 '20 at 13:47
  • 1
    You want to create another make rule. Something like "run" which first builds your proogram and executes it afterwards. That will work. Then you will only need to type ":make run | copen" in vim. – Daniel Heinrich Mar 26 '20 at 14:03
  • @Okay I see can the output now. But what's going on with window at the [bottom](https://imgur.com/a/htFEnbo). Why its showing two pipe characters? – Cody Mar 26 '20 at 15:18
  • 1
    _"Why its showing two pipe characters?"_ That's how Vim displays things in the quickfix window. In there was an error, in between the two pipes, there would be the filename, the line number where the error appears, and may the column number as well. – Luc Hermitte Mar 26 '20 at 17:18

2 Answers2

3
:make && ./hello

Did you try this ?

Alternatively, try below

:make && ./hello | copen
mlwn
  • 1,156
  • 1
  • 10
  • 25
  • when I execute `:make && ./hello | copen`, I see `|| hello world :)`, in the bottom window. Here is the screenshot https://imgur.com/a/2MaCX2U – Cody Mar 26 '20 at 13:37
  • (How did I miss `:make tgt && ./tgt` trick all these years? Well, it ends into the qf window, but this is still quite interesting. Thanks!) – Luc Hermitte Mar 26 '20 at 17:34
1

First of all, you don't need to write a Makefile (unless you have an improper version of gnumake (mingw), or if you are on a non gnu system (macosx?)) for programs made of a single source file.

Then, in another post, I proposed a way to compile and execute (if compilation succeeds) and that permits to inject optional inputs. The solution requires a bit of scripting.

The manual non automated version is

" Change compilation options -- required once per vim session
" in C++, it would be $CXXFLAGS...
" See the other post for more on the topic
let $CFLAGS = '-Wall -Wextra -O3'

" Compile
:make %<
:copen

" Execute
:!./%<
" or
:term ./%<
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • `:./%` gives me "E486: Pattern not found: %". Why is that? – Cody Mar 26 '20 at 17:02
  • Sorry, I forgot the `!` in `:!./%` – Luc Hermitte Mar 26 '20 at 17:15
  • this is giving me "executing job failed: Permission denied" – Cody Mar 26 '20 at 17:17
  • 1
    Ah. I Know. The pattern I gave you applies when the name of the executable is the name of the source file without any extension. Vim automatically expands `%<` into the current filename without its extension. As you have a `Makefile` that overrides defaults (i.e. 'foo.c' is compiled into 'foo'), the file produced isn't 'hellomain', but something else. For `!./%<` to work unconditionally you'd have to either remove your Makefile, or to change the name of the final executable. Of course, all I say, and `%<` make sense **only in the case of monofiles projects**. – Luc Hermitte Mar 26 '20 at 17:29