5

I'm trying to compile a simple c file from an other c program. I call gcc via a short shell script (I know that I can do this directly, but it is a test). Here is my code:

My main program (small piece):

    char strCommand[100];
    sprintf(strCommand, "./compile.sh %s %u", fileName, nr);

    FILE *pipe = popen(strCommand, "r");
    if (pipe == NULL) {
        perror("Unable to open compile script");
        exit(-1);
    }

    char path[LINE_BUFSIZE];
    while (fgets(path, LINE_BUFSIZE, pipe) != NULL)
           printf("%s", path);

    pclose(pipe);

My shell script:

#!/bin/bash 
gcc $1 -o foo-$2

And the file that should be compiled ;-) :

#include <stdio.h>
#include <stdlib.h>

int main(){
    printf("Hello world");
}

When I call the bash script directly from a terminal everything works fine. However when my script is called from the c program the following error occurs:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

This error implies that there is no main function in my Hello World program. It is however clear that there is one. What am I missing?

JOK
  • 135
  • 1
  • 7
  • 1
    Try printf-ing `fileName` - it needs to be valid and have a .c suffix. Also perhaps try printf-ing `strCommand`, just to make sure there is nothing funny going on. – Paul R Jul 31 '14 at 13:11
  • 1
    Take a look at http://stackoverflow.com/questions/7681110/gcc-returning-tons-of-errors-in-a-tiny-hello-world-program?rq=1 your errors are very similar. – Barmar Jul 31 '14 at 13:14
  • 1
    possible duplicate of [Linking error using gcc in Ubuntu 11.10](http://stackoverflow.com/questions/10056724/linking-error-using-gcc-in-ubuntu-11-10) – Barmar Jul 31 '14 at 13:16
  • @Barmar probably not a duplicate - it's the same error message, but probably a different cause – Drew McGowen Jul 31 '14 at 13:23
  • 1
    Try printing the args passed into the script? i.e. `echo '$1='"$1"` and `'$2='"$2"` – Drew McGowen Jul 31 '14 at 13:24
  • @DrewMcGowen Yeah, I probably should have waited until he showed the output of the requested printfs. If he does and it turns out to be significantly different, I'll retract my close vote. – Barmar Jul 31 '14 at 13:29
  • For what it worth, I tried on my Linux Debian Wheezy x86_64 box (gcc 4.7.2). Its works as expected. Any problem with the `fileName` ? Are you sure you are in the _correct_ directory compiling the _right_ sources ? – Sylvain Leroux Jul 31 '14 at 13:53
  • I printed the fileName and the strCommand and they are as expected. (foo.c and ./compile.sh foo.c 1) These arguments are also passed to the shell script correctly (running "./compile.sh foo.c 1" in a terminal works fine). Since all these files are in the same directory, this is probably also not the problem. – JOK Jul 31 '14 at 14:13
  • 2
    I also can't reproduce the problem. Can you provide a complete example, the commands you compile and invoke with and what OS you use? – mafso Jul 31 '14 at 14:29
  • Maybe try adding the `-c` switch in your script invoking `gcc` and see if you get an object file you can link manually afterwards. Maybe the translation only seems to work and you compile an empty file (which then fails to link) or something. – mafso Jul 31 '14 at 15:03
  • 1
    First thing, add `echo "$@"` to the script. – n. m. could be an AI Jul 31 '14 at 15:26
  • Seems to work fine for me. Not able to reproduce the same. Also `gcc $1 -o foo-$2` considering you are accepting `uint` as `$2` (for example `$2` is 10), so you want to name you binary as `foo-10` ? – Saurabh Meshram Sep 03 '14 at 10:16

2 Answers2

1

Make sure your hello world program has a .c extension and not a .cpp (or similar) extension

gcc can compile C++ programs but it does not link in the C++ standard library, it links in the standard C library.

If your program is C++ your main function will be name mangled which will explain why the C library cannot find it.

If you want to compile C++ with the C++ standard library use g++.

doron
  • 27,972
  • 12
  • 65
  • 103
-1

You could try using the system function in C to call the bash script.

system("bash /path/to/compile.sh %s %u");

I use this very simple method often. Just make sure to use #include <stdlib.h> as well.

The error that you are getting is a Linker error. Usually it is caused by not defining int main() in C/C++ code you are trying to run. Though the hello world C code compiles and runs fine, your main C code may be missing the int main() function. Check to make sure the main program has the int main() function and if you still get the linker error, use the system call function which will send a command to the default shell interpreter on your linux box. In Ubuntu this is usually bash. If the shell script then runs while using a system call, then you know you had a mere syntax error causing your linker error.

Yokai
  • 1,170
  • 13
  • 17
  • The answer is basically a "try this instead", which in general is not helpful without further explanations. There is no reason why this would work any better than what the OP already does. And if there are, you should explain why. In addition, this code passes a (s)printf format string to system(), which isn't going work much. – nos Oct 13 '16 at 23:26
  • Well OP made it perfectly clear that his issue was that his C code errored when trying to run the shell script. If he would replace the method he is trying with a simple system call, the script will run perfectly fine with or without ARGs. I use the system C function almost every single day to call external shell scripts. In regards to his need to run a shell script in his C code, the system call is perfect for such simple code he is running. This is a perfectly fine answer for the OP. – Yokai Oct 14 '16 at 05:55
  • While I understand that you have used something similar, and it worked, there are still no reason why your suggestion would work any better than what the OP is already doing - and if you know that there is a specific reason as to why the issue occurs with the code the OP uses, you should state that. The real issue is quite likely not with the code the OP uses, but something different. The gist of it is that suggestions like "try this instead", without further explanation is a low quality answer - even if the suggestion happened to work. – nos Oct 14 '16 at 07:22
  • I have updated the answer to be a bit more precise in my reasoning for using the system call function. – Yokai Oct 14 '16 at 09:25