从 TCL 执行 cpp/exe

Execute cpp/exe from TCL

我会打电话给 TCL call_c_code cpp/exe。 我试过以下代码:

#!/usr/bin/tclsh
set scripts_path call_c_code.exe
exec gcc -c $scripts_path >@stdout 2>@stderr

但是我有以下错误:

% source "tcl_to_call_C.tcl"
gcc.exe: warning: call_c_code.exe: linker input file unused because linking not done

call_c_code.exe 是一个基本的 HelloWorld:

#include <stdio.h>

int main() {
   printf("Hello World!");
   return 0;
}

这是从 TCL 调用“.exe”的正确方法吗?

正在做

 set scripts_path call_c_code.exe
 exec gcc -c $scripts_path >@stdout 2>@stderr

你会

gcc -c call_c_code.exe 

哪里错了,需要注明源文件才能编译

所以可能是这样的

gcc -c call_c_code.c 

然后

 set scripts_path call_c_code.c
 exec gcc -c $scripts_path >@stdout 2>@stderr

但是使用选项 -c 你只生成对象而不是可执行文件,可能是你想要的

 set scripts_path call_c_code.c
 exec gcc  $scripts_path >@stdout 2>@stderr

如果你真的想要扩展 exe 甚至 Windows 添加选项 -o

 set scripts_path call_c_code
 exec gcc -o $scripts_path.exe $scripts_path.c >@stdout 2>@stderr

无论如何隐藏编译器可能产生的消息不是一个好主意,最好是删除重定向,相反添加选项-Wall要求编译器发出更多信号

调用 C 编译器比您习惯的要复杂一些。您需要将您的源代码放在扩展名为 .c 的文件中,然后将其编译为可执行文件(扩展名为 .exe on Windows; other平台有不同的规则!),只有这样你才能 运行 它。当您开始使用库时,还有 很多 其他复杂性。但首先……

  1. 将源代码文件从 call_c_code.exe 重命名为 call_c_code.c
  2. 重写你的编译和运行代码:

    set Compiler "gcc"; # Can override this to be clang if you have it?
    
    proc compile {sourceFile} {
        global Compiler
    
        # Make sure we use the full name of the executable when running it
        set executable [file normalize [file rootname $sourceFile].exe]
        # Only run the compiler if file not executable or source file is newer
        if {
            ![file isfile $executable]
            || ![file executable $executable]
            || [file mtime $sourceFile] > [file mtime $executable]
        } then {   # <<< I like to use 'then' after a multi-line condition!
            exec $Compiler -c -o $executable $sourceFile >@stdout 2>@stderr
        }
        return $executable
    }
    
    set exe [compile call_c_code.c]
    set output [exec $exe]
    puts "The output was: $output"