【发布时间】:2015-01-09 05:58:30
【问题描述】:
我正在 Linux 上编写一个将文本写入文件的 c 程序。我在尝试使用system("gcc fileName.c") 将我创建的新文档编译成可执行文件时遇到了麻烦。该文件正在获取以下输入:
char Msg[100] = {"#include <stdio.h>\nint main();\n\nint main()\n{\n\n\treturn 0;\n}"};
显然它有一个main() 函数,但它仍然给我下面的错误墙:
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 21 has invalid symbol index 22
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
如果我手动编译它会完美运行...
我对 c 很陌生,所以我不确定我错在哪里,任何帮助将不胜感激。
编辑:
文件内容:
#include <stdio.h>
int main();
int main()
{
return 0;
}
使用-c时输出文件内容:
ELF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\A0\00\00\00\00\00\00\004\00\00\00\00\00(\00 \00\00\00GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2\00\00.symtab\00.strtab\00.shstrtab\00.text\00.data\00.bss\00.comment\00.note.GNU-stack\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\004\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00!\00\00\00\00\00\00\00\00\00\00\00\00\004\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00'\00\00\00\00\00\00\00\00\00\00\00\00\004\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00,\00\00\00\00\00\000\00\00\00\00\00\00\004\00\00\00%\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\005\00\00\00\00\00\00\00\00\00\00\00\00\00\00Y\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00Y\00\00\00E\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00p\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00 \00\00\00\00\00\00\00\00\00\00\00\00\00\00x\00\00
\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\F1\FF\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00fffggg.c\00
编辑: 完整代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char nepCode[10];
char PathOut[1035];
char Msg[100] = {"#include <stdio.h>\nint main();\n\nint main()\n{\n\n\treturn 0;\n}"};
FILE *fp;
char path[1035];
char fullPath[1035];
/* Open the command for reading. */
fp = popen("/bin/pwd", "r");
if (fp == NULL)
{
printf("Failed to run command\n" );
exit(1);
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL)
{
int i = 0;
printf("Current working directory: %s", path);
for(i = 0; i < 1035; i++)
{
PathOut[i] = path[i];
}
}
printf("//Program by WolfyD\n//Creates a .c template file with your Neptun Code \n//in the folder from where it's run from");
printf("\n\n\nPlease enter your NEPTUN CODE: ");
nepCode[0] = '/';
scanf("%s",nepCode);
nepCode[6] = '.';
nepCode[7] = 'c';
int index = 0;
int i2 = -1;
for(index = 0; index < 1035; index++)
{
if(PathOut[index] != '\0')
{
fullPath[index] = PathOut[index];
}
else
{
if(i2 == -1)
{
index--;
fullPath[index] = '/';
}
else
{
fullPath[index] = nepCode[i2];
}
i2++;
}
}
FILE *f = fopen(fullPath, "ab");
fputs(Msg,f);
printf("File [ %s ] written to: %s\n\n",nepCode, fullPath);
sleep(2);
char sysStr[100] = {"gcc -oofile.o -c "};
int i = 0;
for(i = 0; i < strlen(fullPath); i++)
{
sysStr[17 + i] = fullPath[i];
}
printf("%s\n\n\n",sysStr);
system(sysStr);
printf("\nOutput file [ ofile.o ] created\n\n");
system("gcc -o output ofile.o");
/* close */
pclose(fp);
return 0;
}
请不要评价太苛刻,我只是学c
编辑:
更新:
我决定在我的 Win8 PC 上尝试代码,我意识到至少我的一个问题是一些垃圾,但现在我稍微编辑了它。在 Windows 上,我有 MinGW,当我尝试将 o 文件编译为可执行文件时,它会写入不同的消息:
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../libmingw32.a(main.o):main.c:(.tex
t.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
【问题讨论】:
-
与你的问题无关,但是为什么代码在指针数组中呢?如果你把它放在一个数组中会不会更容易,比如例如
char Msg[] = "...";? -
关于您的问题的更多信息,您检查过输出文件吗?你试过手动编译吗?
-
谢谢,我对所有数组有点困惑,我解决了这个问题,但问题还是一样。是的,我以多种方式手动尝试过,如果我使用 -c 它会创建一个目标文件,但我仍然无法将其编译为可执行文件。
-
可能
system生成的shell使用的路径和你普通shell中的路径不同,导致system调用的gcc命令和调用的不一样由您手动执行。 -
这是我的猜测,但我怎么知道它有什么不同呢?如果我用
printf()或echo写出来,它看起来和我写的完全一样,即使我把它复制出来并手动使用
标签: c linux gcc compilation terminal