Compiling c code programatically in linux terminal gcc

喜欢而已 提交于 2020-01-13 19:52:10

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/26886020/compiling-c-code-programatically-in-linux-terminal-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!