public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Issues with LD when using GCC in a C program
@ 2009-03-24  4:45 Marcus Clyne
  2009-03-24 14:17 ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Marcus Clyne @ 2009-03-24  4:45 UTC (permalink / raw)
  To: gcc-help

Hi,

I'm writing a C program which calls gcc using execl().

I have successfully called gcc to create an object file, but when I try 
to create the shared object file, I get problems with ld.

The error I'm getting is:
------------------
/usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crt1.o: In function 
`_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
------------------

I have added a PATH and LD_LIBRARY_PATH in the evironment, but this 
doesn't solve the problem.

The relevant code is:
-------------------
setenv ("PATH", 
"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games", 
1);
setenv ("LD_LIBRARY_PATH", 
"/usr/local/lib:/usr/lib:/lib:/lib/tls/i686/cmov", 1);


// compile the shared object

execl ("/usr/bin/gcc",
           "-shared",
           "-lmylib",
           "-o",
           so_file,
           object_file,
           NULL);
-------------------

Where so_file and object_file are NULL-terminated char*'s, and the 
object_file has been created.

I can successfully compile the files using gcc from my shell, and ld is 
located at /usr/bin/ld.

I'm guessing that I need to add something to my env, or perhaps to the 
gcc options, but I'm not quite sure what.


Can anyone help?

Thanks,

Marcus Clyne.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issues with LD when using GCC in a C program
  2009-03-24  4:45 Issues with LD when using GCC in a C program Marcus Clyne
@ 2009-03-24 14:17 ` Ian Lance Taylor
  2009-03-30  9:27   ` Philip Herron
  0 siblings, 1 reply; 4+ messages in thread
From: Ian Lance Taylor @ 2009-03-24 14:17 UTC (permalink / raw)
  To: eugaia; +Cc: gcc-help

Marcus Clyne <eugaia@gmail.com> writes:

> // compile the shared object
>
> execl ("/usr/bin/gcc",
>           "-shared",
>           "-lmylib",
>           "-o",
>           so_file,
>           object_file,
>           NULL);

Note that -lmylib is in the wrong place.  Libraries must follow the
objects which refer to them.

It does not make sense that you would get an undefined symbol error when
using -shared, so I don't know what is going on.  It is unlikely that
you are missing anything from the environment.

I recommend adding the -v option to your execl, to verify that the
expected options are being passed to the subprocesses.  Compare to the
output using -v on the command line.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issues with LD when using GCC in a C program
  2009-03-24 14:17 ` Ian Lance Taylor
@ 2009-03-30  9:27   ` Philip Herron
  2009-03-30 17:43     ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Philip Herron @ 2009-03-30  9:27 UTC (permalink / raw)
  To: Ian Lance Taylor; +Cc: eugaia, gcc-help

Hey

Instead of using execl you could also use: popen

http://linux.die.net/man/3/popen

Brings you back the outputs like stdout stderr etc...

-Phil

2009/3/24 Ian Lance Taylor <iant@google.com>:
> Marcus Clyne <eugaia@gmail.com> writes:
>
>> // compile the shared object
>>
>> execl ("/usr/bin/gcc",
>>           "-shared",
>>           "-lmylib",
>>           "-o",
>>           so_file,
>>           object_file,
>>           NULL);
>
> Note that -lmylib is in the wrong place.  Libraries must follow the
> objects which refer to them.
>
> It does not make sense that you would get an undefined symbol error when
> using -shared, so I don't know what is going on.  It is unlikely that
> you are missing anything from the environment.
>
> I recommend adding the -v option to your execl, to verify that the
> expected options are being passed to the subprocesses.  Compare to the
> output using -v on the command line.
>
> Ian
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issues with LD when using GCC in a C program
  2009-03-30  9:27   ` Philip Herron
@ 2009-03-30 17:43     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2009-03-30 17:43 UTC (permalink / raw)
  To: Philip Herron; +Cc: eugaia, gcc-help

Philip Herron <herron.philip@googlemail.com> writes:

> Instead of using execl you could also use: popen
>
> http://linux.die.net/man/3/popen
>
> Brings you back the outputs like stdout stderr etc...

Note that popen, unlike execl, runs the shell, which leads to various
security issues.  execl is definitely to be preferred when there is any
user control over arguments or the environment.  That may not be an
issue in this particular case, of course.

Ian



> 2009/3/24 Ian Lance Taylor <iant@google.com>:
>> Marcus Clyne <eugaia@gmail.com> writes:
>>
>>> // compile the shared object
>>>
>>> execl ("/usr/bin/gcc",
>>>           "-shared",
>>>           "-lmylib",
>>>           "-o",
>>>           so_file,
>>>           object_file,
>>>           NULL);
>>
>> Note that -lmylib is in the wrong place.  Libraries must follow the
>> objects which refer to them.
>>
>> It does not make sense that you would get an undefined symbol error when
>> using -shared, so I don't know what is going on.  It is unlikely that
>> you are missing anything from the environment.
>>
>> I recommend adding the -v option to your execl, to verify that the
>> expected options are being passed to the subprocesses.  Compare to the
>> output using -v on the command line.
>>
>> Ian
>>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-03-30 17:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-24  4:45 Issues with LD when using GCC in a C program Marcus Clyne
2009-03-24 14:17 ` Ian Lance Taylor
2009-03-30  9:27   ` Philip Herron
2009-03-30 17:43     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).