drh / lcc

The lcc retargetable ANSI C compiler
https://drh.github.io/lcc/
2.03k stars 442 forks source link

Compiling to Assembly not Linux #20

Closed npateman closed 9 years ago

npateman commented 9 years ago

Hi there,

I have probably not done this correctly, but for some reason I'm trying to do a simple helloworld app and generate a linux x86 binary, unfortunately it's generating what appears to be assembly.

Following instructions from this site,

http://drh.github.io/lcc/current/doc/install.html

I've compiled both driver and compiler, and then created a simple hello world app, i.e.


include ⟨stdio.h⟩

int main() { printf("Hello World!"); return(0); }


compiling with gcc works great, I run it and get the text printed on screen. But when I compile with lcc, using this command,

./rcc main.c -target=x86/linux -o helloworld

a file gets produced called helloworld, but it contains assembly code, for example,


.globl main .text.align 16 .type main,@function main: ...


Any ideas what I'm doing wrong?

npateman commented 9 years ago

btw i tried lcc, got this error,

"./lcc: /usr/local/lib/lcc/gcc/cpp: No such file or directory"

not sure why one to use or how to use this compiler.

ksherlock commented 9 years ago

etc/linux.c is the compiler driver (lcc) which calls cpp, rcc, as, and ld.

The install.html file has information on installing it but I wouldn't trust gcc's headers (stdarg.h, for example, uses gcc builtin functions that aren't supported on lcc). Older versions are probably safer.

Anyhow, try this:

  1. make a /usr/local/lib/lcc/ directory and put rcc in there.
  2. symlink /usr/local/lib/lcc/gcc/ to your gcc library dir (e.g., /usr/lib/gcc/arm-linux-gnueabi/4.9/)
  3. If your gcc dir doesn't have a cpp in it, create a symlink (to /usr/bin/cpp) in the gcc dir, or edit the etc/linux.c driver to use /usr/bin/cpp instead of LCCDIR "/gcc/cpp"
npateman commented 9 years ago

Hi ksherlock,

Please forgive me, not entirely sure I've understood you correctly and me not being a linux guru is not helping. I removed my entire folder for lcc and started again from scratch, for some reason this time my BUILDDIR environment variable didn't work and severa attempts to get it working again have failed. So it's now dumping everything to the root "/".

Anyway so after compiling from my /home/nick/lcc/ directory using

sudo make HOSTFILE=etc/linux.c lcc

It dumps a load of files in root, I then do

sudo make all

and it dumps a load more files in root,

I then done as suggested, moving rcc to "/usr/local/lib/lcc/", created a symlink to gcc in there, and also a symlink to cpp in the gcc folder (symlink). I then attempt to compile my main.c file again, as outlined above,

/./lcc main.c

output:

<command line>:1:1: warning: "STDC" redefined <built-in>: warning: this is the location of the previous definition /usr/bin/ld: cannot find --llcc

npateman commented 9 years ago

also just to clarify, I only copied "rcc" into the new dir /usr/local/lib/lcc, should I have put everything in there?

npateman commented 9 years ago

I now have everything being compiled into the directory you told me to put rcc in, added the path to the path environment variable then attempted compiling again. The same error occurs.

npateman commented 9 years ago

also sorry, another thing, my hello world app is now super simple, not really a hello world app anymore but a starting point for compilation using lcc,


int main() { return(0); }


npateman commented 9 years ago

I'm running through the instructions to perform the "make triple test". This processes for a little and then exits with a bunch of "cc: no input files" errors. Then if I run it again I get a single error of the same type,

# sudo make triple BUILDDIR?=/home/nick/lcc/lcc-master/out cc -g -c -o /home/nick/lcc/lcc-master/out/host.o cc: no input files make : *\ [/home/nick/lcc/lcc-master/out/host.o] Error 1

I'm currently trying this in CentOS 5.11 as the code I need to compile to MIPS is rather old.

npateman commented 9 years ago

Okay I started from scratch again, compiled to everything in order to my /home/nick/lcc/lcc-master/out directory,

so that is,

% make BUILDDIR="/home/nick/lcc/lcc-master/out" HOSTFILE="etc/linux.c" lcc % make BUILDDIR="/home/nick/lcc/lcc-master/out" all

Then I made a symbolic link to gcc in the out folder and a symbolic link to "/usr/bin/cpp" inside of that, which already existed from last time. And then tried to compile the triple test,

% make BUILDDIR="/home/nick/lcc/lcc-master/out" TARGET=x86/linux triple 2> out.txt

The output of which I have linked below, the entire build seems to fail from the start.

http://npsoftware.s3.amazonaws.com/out.txt

Sorry for this massive influx of messages since your last reply, I really need to get this working and you're my only avenue for assistance at current. I really appreciate your time!

ksherlock commented 9 years ago

You also need to copy the headers from include/x86/linux/.

/usr/local/lib/lcc/rcc /usr/local/lib/lcc/liblcc.a /usr/local/lib/lcc/gcc -> symbolic link to /usr/lib/gcc/i386-redhat-linux/4.1.2/ /usr/local/lib/lcc/include/assert.h /usr/local/lib/lcc/include/float.h /usr/local/lib/lcc/include/limits.h /usr/local/lib/lcc/include/math.h /usr/local/lib/lcc/include/stdarg.h

You could also delete the -D__STDC__=1 from etc/linux.c line 11 to get rid of the "__STDC__" redefined warning

npateman commented 9 years ago

Hi thanks and sorry for my delay, I resorted to using openwrt in the end ands Buildroot which nicely set up everything for me in order to achieve what i was after with lcc, thanks for your time it is much appreciated.