twibiral / obsidian-execute-code

Obsidian Plugin to execute code in a note.
MIT License
1.06k stars 67 forks source link

C Language on Windows support #276

Open Welding-Torch opened 1 year ago

Welding-Torch commented 1 year ago

Description

The existing way for this extension to run C Language code is to install Cling. The only issue? Cling does not support the Windows OS. This is a pretty significant problem, since a LOT of people use Windows.

Suggested Solution

Perhaps this extension can add support for different C Language compilers, especially ones that work on Windows. Perhaps GCC or MSVC, among others.

Note An example of someone else running into this issue is here #229.

thunderbolt1231 commented 10 months ago

I have managed to install cling through conda on windows11, and get it running successfully on my powershell terminal. But it doesn't work in Obsidian..

C:\Users\pan>cling

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <stdio.h>
[cling]$ printf("dsfs");
dsfs
[cling]$

in obsidian

Cpp
#include <stdio.h>
int main()
{
    printf("dsfsd");
    return 0;
}

click run:

In file included from input_line_1:1:
In file included from D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\new:6:
In file included from D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\exception:8:
In file included from D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\type_traits:6:
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\xstddef:338:2: error: 'auto' return without trailing return type; deduced return types are a C++14 extension
        auto _Unfancy(_Ptrty _Ptr)
        ^
In file included from input_line_3:1:
In file included from C:\Users\pan\AppData\Local\Temp\temp_1698973417226.cpp:6:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:12:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt.h:10:
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vcruntime.h:184:30: error: typedef redefinition with different types ('unsigned int' vs 'unsigned long long')
    typedef unsigned int     size_t;
                             ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vcruntime.h:196:13: error: unknown type name '_Bool'
    typedef _Bool __vcrt_bool;
            ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vcruntime.h:215:22: error: 'short wchar_t' is invalid
    typedef unsigned short wchar_t;
                     ^
In file included from input_line_3:1:
In file included from C:\Users\pan\AppData\Local\Temp\temp_1698973417226.cpp:6:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:12:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt.h:156:13: error: unknown type name '_Bool'
    typedef _Bool __crt_bool;
            ^
In file included from input_line_3:1:
In file included from C:\Users\pan\AppData\Local\Temp\temp_1698973417226.cpp:6:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_stdio_config.h:31:10: error: Unsupported architecture
        #error Unsupported architecture
         ^
In file included from input_line_3:1:
In file included from C:\Users\pan\AppData\Local\Temp\temp_1698973417226.cpp:6:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:320:16: error: no matching function for call to '_vfwprintf_l'
        return _vfwprintf_l(_Stream, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:296:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t' (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:351:20: error: no matching function for call to '_vfwprintf_s_l'
            return _vfwprintf_s_l(_Stream, _Format, NULL, _ArgList);
                   ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:325:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t' (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_s_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:382:16: error: no matching function for call to '_vfwprintf_p_l'
        return _vfwprintf_p_l(_Stream, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:358:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t' (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_p_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:409:16: error: no matching function for call to '_vfwprintf_l'
        return _vfwprintf_l(stdout, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:296:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t' (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:438:20: error: no matching function for call to '_vfwprintf_s_l'
            return _vfwprintf_s_l(stdout, _Format, NULL, _ArgList);
                   ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:325:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t' (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_s_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:467:16: error: no matching function for call to '_vfwprintf_p_l'
        return _vfwprintf_p_l(stdout, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:358:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t' (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_p_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:483:9: error: use of undeclared identifier '__crt_va_start_a'
        __crt_va_start(_ArgList, _Locale);
        ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
In file included from input_line_3:1:
In file included from C:\Users\pan\AppData\Local\Temp\temp_1698973417226.cpp:6:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:485:9: error: use of undeclared identifier '__crt_va_end'
        __crt_va_end(_ArgList);
        ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:501:9: error: use of undeclared identifier '__crt_va_start_a'
        __crt_va_start(_ArgList, _Format);
        ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
In file included from input_line_3:1:
In file included from C:\Users\pan\AppData\Local\Temp\temp_1698973417226.cpp:6:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:502:19: error: no matching function for call to '_vfwprintf_l'
        _Result = _vfwprintf_l(_Stream, _Format, NULL, _ArgList);
                  ^~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:296:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t' (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:503:9: error: use of undeclared identifier '__crt_va_end'
        __crt_va_end(_ArgList);
        ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:520:9: error: use of undeclared identifier '__crt_va_start_a'
        __crt_va_start(_ArgList, _Locale);
        ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
In file included from input_line_3:1:
In file included from C:\Users\pan\AppData\Local\Temp\temp_1698973417226.cpp:6:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:522:9: error: use of undeclared identifier '__crt_va_end'
        __crt_va_end(_ArgList);
        ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:540:13: error: use of undeclared identifier '__crt_va_start_a'
            __crt_va_start(_ArgList, _Format);
            ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]

my setting image

Welding-Torch commented 10 months ago

@twibiral

twibiral commented 10 months ago

Hi,

Even though I largely use Windows, I only ever used C++ on Linux. I don't know a lot about C++ for Windows. First of all: Yes, you can use Cling on Windows, but there is no documentation about that in the Readme. Feel free to add information you found helpful to the Readme to help other users.

You need to give me extensive details on how you can interactively use C++ on Windows, then I can maybe implement it. I need at least the following information:

The plugin does not (yet) support compiling snippets before running them.

thunderbolt1231 commented 10 months ago

Hi,

Even though I largely use Windows, I only ever used C++ on Linux. I don't know a lot about C++ for Windows. First of all: Yes, you can use Cling on Windows, but there is no documentation about that in the Readme. Feel free to add information you found helpful to the Readme to help other users.

You need to give me extensive details on how you can interactively use C++ on Windows, then I can maybe implement it. I need at least the following information:

  • How to download
  • How to run
  • What are the important command line options (e.g. the C++ version)
  • A working code snippet with the right command to execute it

The plugin does not (yet) support compiling snippets before running them.

I finnally get the cling run correctly on windows by changing some code, it looks like -std=cxx argument doesn't work for cling in windows,

(base) PS F:\study\c> cling test.c
sdfsd(int) 0
(base) PS F:\study\c> cat test.c
#include <stdio.h>

int test()
{
        printf("sdfsd");
        return 0;
}
(base) PS F:\study\c> cling test.c
sdfsd(int) 0
(base) PS F:\study\c> cling -std=c++11 test.c
In file included from input_line_1:1:
In file included from D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\new:6:
In file included from D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\exception:8:
In file included from D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\type_traits:6:
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\xstddef:338:2: error: 'auto' return without trailing return type; deduced return types are a C++14 extension
        auto _Unfancy(_Ptrty _Ptr)
        ^
In file included from input_line_3:1:
In file included from F:\study\c\test.c:1:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:12:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt.h:10:
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vcruntime.h:184:30: error: typedef redefinition with different types ('unsigned int' vs 'unsigned long long')
    typedef unsigned int     size_t;
                             ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vcruntime.h:196:13: error: unknown type name '_Bool'
    typedef _Bool __vcrt_bool;
            ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vcruntime.h:215:22: error: 'short wchar_t' is invalid
    typedef unsigned short wchar_t;
                     ^
In file included from input_line_3:1:
In file included from F:\study\c\test.c:1:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:12:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt.h:156:13: error: unknown type name '_Bool'
    typedef _Bool __crt_bool;
            ^
In file included from input_line_3:1:
In file included from F:\study\c\test.c:1:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_stdio_config.h:31:10: error: Unsupported architecture
        #error Unsupported architecture
         ^
In file included from input_line_3:1:
In file included from F:\study\c\test.c:1:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:320:16: error: no matching function for call to '_vfwprintf_l'
        return _vfwprintf_l(_Stream, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:296:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t'
      (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:351:20: error: no matching function for call to '_vfwprintf_s_l'
            return _vfwprintf_s_l(_Stream, _Format, NULL, _ArgList);
                   ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:325:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t'
      (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_s_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:382:16: error: no matching function for call to '_vfwprintf_p_l'
        return _vfwprintf_p_l(_Stream, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:358:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t'
      (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_p_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:409:16: error: no matching function for call to '_vfwprintf_l'
        return _vfwprintf_l(stdout, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:296:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t'
      (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:438:20: error: no matching function for call to '_vfwprintf_s_l'
            return _vfwprintf_s_l(stdout, _Format, NULL, _ArgList);
                   ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:325:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t'
      (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_s_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:467:16: error: no matching function for call to '_vfwprintf_p_l'
        return _vfwprintf_p_l(stdout, _Format, NULL, _ArgList);
               ^~~~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:358:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t'
      (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_p_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:483:9: error: use of undeclared identifier '__crt_va_start_a'
        __crt_va_start(_ArgList, _Locale);
        ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
In file included from input_line_3:1:
In file included from F:\study\c\test.c:1:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:485:9: error: use of undeclared identifier '__crt_va_end'
        __crt_va_end(_ArgList);
        ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:501:9: error: use of undeclared identifier '__crt_va_start_a'
        __crt_va_start(_ArgList, _Format);
        ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
In file included from input_line_3:1:
In file included from F:\study\c\test.c:1:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:502:19: error: no matching function for call to '_vfwprintf_l'
        _Result = _vfwprintf_l(_Stream, _Format, NULL, _ArgList);
                  ^~~~~~~~~~~~
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:296:37: note: candidate function not viable: cannot convert argument of incomplete type 'void *' to 'const _locale_t'
      (aka '__crt_locale_pointers *const') for 3rd argument
    _CRT_STDIO_INLINE int __CRTDECL _vfwprintf_l(
                                    ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:503:9: error: use of undeclared identifier '__crt_va_end'
        __crt_va_end(_ArgList);
        ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:520:9: error: use of undeclared identifier '__crt_va_start_a'
        __crt_va_start(_ArgList, _Locale);
        ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
In file included from input_line_3:1:
In file included from F:\study\c\test.c:1:
In file included from D:\Windows Kits\10\Include\10.0.17763.0\ucrt\stdio.h:13:
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:522:9: error: use of undeclared identifier '__crt_va_end'
        __crt_va_end(_ArgList);
        ^
D:\Windows Kits\10\Include\10.0.17763.0\ucrt\corecrt_wstdio.h:540:13: error: use of undeclared identifier '__crt_va_start_a'
            __crt_va_start(_ArgList, _Format);
            ^
D:\software\vs2017\VC\Tools\MSVC\14.16.27023\include\vadefs.h:160:35: note: expanded from macro '__crt_va_start'
    #define __crt_va_start(ap, x) __crt_va_start_a(ap, x)
                                  ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]

while there are other wierd io issues such cout not outputting: image but on the otherhand printf works correctly: image

I would suggest we add the gcc/clang compiler option for c/c++ for the sake of windows users.

KM911 commented 9 months ago

Greetings, I am developing a samll adaptor for obsidian-execute-code to support C/C++. You could use g++ or clang++ to compile and run your code instead of cling. More details in obsidian-execute-code-c-support I hope this could solve your issue.

Welding-Torch commented 9 months ago

@KM911 Hey why do you have a .exe file in your releases? This is an obsidian extension, right?

KM911 commented 9 months ago

@Welding-Torch , this .exe file is like cling that can run c/cpp in code block. You need to download it and add it's path to the cling path tab. image

SlimReaper15 commented 9 months ago

@KM911 Is it possible to receive user input while executing code? Currently, statements like cout << "Hello World" work as they're supposed to but I'm unable to receive user inputs using cin.

KM911 commented 9 months ago

I've made some minor adjustments to match your request. Here are the results. code

SlimReaper15 commented 9 months ago

This works perfectly, thanks a lot!

TanDMoss commented 7 months ago

I've made some minor adjustments to match your request. Here are the results. code code

@KM911 This looks amazing. I installed it on my Windows computer and it works perfectly. Any chance this would work for Mac as well?

KM911 commented 7 months ago

Although Go is a cross-platform language, I have been using cmd to execute certain commands. This will not work on macOS. Unfortunately, I have never used Mac before :cry: (poor) . You may need to modify the exec.Command section of the code.
I've attempted to use KVM and Docker-osx, but my Windows version is outdated and lacks systemd support.
If provide a detailed guide on installing macOS, I am willing to attempt the installation and potentially enable macOS compatibility. :heart: