且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Linux终端的gcc编译C code编程

更新时间:2023-11-13 13:23:34

我没有完全按照你的逻辑,把这里有一些可能出现的问题:

所有的,你在哪里得到的路径首先;这个程序可以使用命令行参数比它是你是如何做的更简单。

这意味着,你声明主要在你的主文件如下:
INT主(INT ARGC,CHAR *的argv [])

在这种形式下,ARGC是包括文件名称传递参数的个数,和argv [0]是一个字符串,牵着你的文件的名称。

因此​​,而不是依靠程序来为您生成的文件,你这样做有点像这样的:

 的#includestdlib.h中
的#includestdio.h中
的#includestring.h中
INT主(INT ARGC,CHAR *的argv []){
  如果(argc个!= 3){
    fprintf中(标准错误,用法:%s%S \\ N的argv [0],INPUT_FILE OUTPUT_FILE);
    fprintf中(标准错误,不添加扩展至output_file \\ n);
    返回EXIT_FAILURE;
  }
  unsigned int类型I = 0;
  对于(I = 0; I&下; strlen的(的argv [2]);我++){
    如果(的argv [2] [I] =='\\ n'||的argv [2] [I] =='\\ 0'){
      的argv [2] [我] ='\\ 0';
      打破;
    }
  }
  烧焦FORMAT_STRING [150];
  sprintf的(FORMAT_STRINGgcc.exe -Wall -Wextra -c%s的-o%s.o的argv [1],ARGV [2]);
  焦炭format_string2 [150];
  sprintf的(format_string2,gcc.exe%S -o%s.exe的argv [1],ARGV [2]);
  系统(FORMAT_STRING);
  系统(format_string2);
  返回EXIT_SUCCESS;
}

通过上面的程序,你只需要创建一个输入文件,传递inpu文件作为第一个参数,你的输出到第二个。该文件确实让两个假设:INPUT_FILE存在和路径GCC是在环境的PATH变量

最后,不要忘记,主并不需要的原型。

I'm writing a c program on Linux that writes text into a file. I'm having trouble where i'M trying to use system("gcc fileName.c") to compile the new document i created into an executable. The file is getting the following input:

char Msg[100] = {"#include <stdio.h>\nint main();\n\nint main()\n{\n\n\treturn 0;\n}"};

clearly it has a main() function and yet it still gives me the following wall of error:

/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

If I compile it manually it works perfectly...

I'm very new to c so I'm not sure where I'm wrong, any help would be appreciated.

EDIT:

File contents:

#include <stdio.h>
int main();

int main() 
{

     return 0;
}

when used -c output file contents:

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

EDIT: Full Code:

#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;

}

Please don't judge too harshly, I'm just learning c

EDIT:

update:

I decided to try the code out on my Win8 PC and i realized at least one of my problems was some garbage, but now i edited it slightly. On Windows i have MinGW and it writes a different message when i try to compile the o file into an executable:

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

I'm not completely following your logic, put here are some possible problems:

First of all, where do you get the path from; this program may be simpler using command-line arguments than it is how you are doing it.

This means, you declare main as follows in your main file: int main( int argc, char *argv[] )

In this form, argc is the number of arguments passed INCLUDING the file name, and argv[0] is a string holding your file's name.

So, rather than rely on the program to generate the file for you, you do it somewhat like this:

#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;
}

With the above program, you just create the input file then pass the inpu file as the first argument and your output to the second one. This file does make two assumptions: input_file exists and the path to gcc is in your enviroment's path variable.

Finally, don't forget that main does NOT need a prototype.