从 python 脚本调用 gcc 给我“未定义的符号:”_main”

发布于 2024-10-04 08:20:43 字数 1166 浏览 4 评论 0原文

我正在尝试用 Python 编写一个代码生成器脚本,该脚本生成一个 C 源文件,编译并运行它。但是,我在从脚本调用 gcc 时遇到问题。

一个简单的 hello world 示例:

import subprocess  

basename = "CodeGenTest";  
execname = basename;  
srcname = basename + ".c";  

codeList = [];  
codeList.append("#include <stdio.h>");  
codeList.append("int main(int argc, char *argv[])\n{");  
codeList.append("printf(\"Hello world.\\n\");");  
codeList.append("}");  

# Convert codelist to string.  
codeList.append("");  
codeString = "\n".join(codeList);  

# Print code to output source file  
outfile=open(srcname,'w');  
outfile.write(codeString);  
outfile.close;  

print "Compile.";  
cmd = ["gcc", "-O2", srcname, "-o", execname];  
p = subprocess.Popen(cmd);  
p.wait();  

subprocess.call(["./"+execname]);  

如果我运行此脚本,我会得到以下错误输出

Compile.
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

如果我在 python 解释器 shell 中执行完全相同的操作,则它可以正常工作。我也可以直接在shell中编译代码,没有问题。

我尝试了各种变体,使用 subprocess.Popen()、subprocess.call(),无论有没有我能想到的所有可能的参数组合,仍然是同样的问题。

有人知道我这里可能有什么问题吗?

I am trying to write a code generator script in Python, that generates a C-source file, compiles it and runs it. However, I am having trouble calling gcc from my script.

A simple hello world example:

import subprocess  

basename = "CodeGenTest";  
execname = basename;  
srcname = basename + ".c";  

codeList = [];  
codeList.append("#include <stdio.h>");  
codeList.append("int main(int argc, char *argv[])\n{");  
codeList.append("printf(\"Hello world.\\n\");");  
codeList.append("}");  

# Convert codelist to string.  
codeList.append("");  
codeString = "\n".join(codeList);  

# Print code to output source file  
outfile=open(srcname,'w');  
outfile.write(codeString);  
outfile.close;  

print "Compile.";  
cmd = ["gcc", "-O2", srcname, "-o", execname];  
p = subprocess.Popen(cmd);  
p.wait();  

subprocess.call(["./"+execname]);  

If I run this script, I get the following error output

Compile.
Undefined symbols:
  "_main", referenced from:
      start in crt1.10.6.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

If I do the exact same thing in the python interpreter shell, it works fine. I can also compile the code without problems directly in the shell.

I have tried various variants, using subprocess.Popen(), subprocess.call(), with and without all possible combinations of arguments that I can think of, still the same problem.

Anyone has any idea what might be my problem here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

中性美 2024-10-11 08:20:43

将其更改

outfile.close;

为:

outfile.close()

您实际上并没有关闭文件,因此 Python 不会刷新其缓冲区,因此源文件中的所有内容都是空文件。当 gcc 编译空文件时,它会抱怨没有 main 函数作为程序的入口点。

我还建议您检查 p.returncode 是否为 0,以确保 gcc 在尝试执行(可能不存在)输出二进制文件之前成功。

此外,不需要以分号结束每个语句。如果每行有多个语句,则只需要一个分号,在这种情况下,您需要在语句之间使用它们。当前面没有反斜杠时,行尾服务器作为语句终止符。

Change this

outfile.close;

to this:

outfile.close()

You're not actually closing the file, so Python isn't flushing its buffers, so all that's in the source file is an empty file. When gcc compiles an empty file, it complains that there's no main function to serve as the program's entry point.

I'd also suggest that you check that p.returncode is 0 to ensure that gcc succeeded before trying to execute the (possibly non-existant) output binary.

Also there's no need to end each statement with a semicolon. You only need a semicolon if you have multiple statements per line, in which case you need them in between statements. End-of-line servers as a statement terminator when not preceded by a backslash.

错爱 2024-10-11 08:20:43

您实际上并没有调用 outfile.close;它应该是outfile.close()。很有可能源代码仍然卡在某个缓冲区中,而 GCC 没有看到它。

You're not actually calling outfile.close; it should be outfile.close(). Good chance the source is still stuck in a buffer somewhere, and GCC doesn't see it.

皓月长歌 2024-10-11 08:20:43

您可以通过使用 with-block 来管理文件来避免该问题:

with file(srcname, 'w') as outfile:
    outfile.write(codeString)

另请注意,Python 中不需要分号,除非您在同一行上编写多个语句。

You could avoid the problem by using a with-block to manage the file:

with file(srcname, 'w') as outfile:
    outfile.write(codeString)

Also please note that semicolons are not required in Python unless you are writing multiple statements on the same line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文