在linux终端gcc中以编程方式编译c代码

时间:2014-11-12 11:36:59

标签: c linux gcc compilation terminal

我正在Linux上编写一个将文本写入文件的程序。我在尝试使用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

1 个答案:

答案 0 :(得分:1)

我没有完全遵循你的逻辑,这里有一些可能的问题:

首先,你从哪里获得路径;使用命令行参数可能比使用它的方式更简单。

这意味着,您在主文件中声明main如下: int main(int argc,char * argv [])

在这种形式中,argc是包含文件名的参数数量,argv [0]是一个包含文件名的字符串。

所以,不要依赖程序为你生成文件,你可以这样做:

#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int main( int argc, char *argv[] ) {
  if (argc != 3) {
    fprintf( stderr, "Usage: %s %s\n", argv[0], "input_file output_file" );
    fprintf( stderr, "Do not add an extension to output_file.\n" );
    return EXIT_FAILURE;
  }
  unsigned int i = 0;
  for (i = 0; i < strlen( argv[2] ); i++) {
    if (argv[2][i] == '\n' || argv[2][i] == '\0') {
      argv[2][i] = '\0';
      break;
    }
  }
  char format_string[150];
  sprintf( format_string, "gcc.exe -Wall -Wextra -c %s -o %s.o", argv[1], argv[2] );
  char format_string2[150];
  sprintf( format_string2, "gcc.exe %s -o %s.exe", argv[1], argv[2] );
  system( format_string );
  system( format_string2 );
  return EXIT_SUCCESS;
}

使用上面的程序,您只需创建输入文件,然后将inpu文件作为第一个参数传递,将输出传递给第二个参数。这个文件做了两个假设:input_file存在,gcc的路径在你的环境的路径变量中。

最后,不要忘记main不需要原型。