0

I'm working on porting a windows project to linux so I am trying to learn/setup my build pipeline on linux(ubuntu). I use clang on windows and would like to use clang/llvm to compile my code on ubuntu but I am having an issue linking the c runtime library to my code. Here's an example of what I am trying to run just so I can set up the build scripts,

// -- system includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// -- my includes
#include "crt.h"

int main() {
char title[] = "Text";
int title_length = strlen(title);

// -- sending string and length to platform dependent code
CreateBox(title, title_length);

return(0); }

After installing clang 3.4 on Ubuntu 14.04 LTS I run the above code as well as platform dependent code from the terminal,

clang++ -c main.cpp box_linux.cpp

The file compiles without error. Let's say I had I use the following command when trying to link,

ld main.o box.o

I get back the following error every single time,

ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
In function 'main':
(.text+0x30): undefined reference to 'strlen'

I try linking libc++, libstdc++ but nothing seems to work. I am admittedly very, very new to linux and clang on linux so I apologize in advance. I have checked different questions here on stackoveflow as well as ubuntu's forums and LLVM's forums/docs to no avail. If anyone could point me in the right direction it would be greatly appreciated.

Edited Content
  • 187
  • 1
  • 2
  • 10

1 Answers1

3

If you are using clang (or gcc) on linux you don't have to do the linking on your own. You can let clang do the linking for you and it will select the necessary libraries for your system just remove the -c flag form command line or use clang main.o. Clang understands that a .o file is already an object file and passes it on to the linker.

If you realy want to invoke ld on your own then start with clang++ -v main.cpp. The -v switch let clang print the invocation command for ld which e.g., looks like this:

/usr/bin/ld" -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. -L/data/home/user/bin/../lib -L/lib -L/usr/lib /tmp/test-574b88.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o

on my Ubuntu 14.04 LTS.

Michael Haidl
  • 5,384
  • 25
  • 43
  • Thank you for the quick response! And I should have included an additional file or at least a mention to it. That was my mistake. Because I am trying to make my code platform independent I have my entry point calling a common file that handles all of the platform independent functions(i/o, grraphics, etc). As far as I know, clang and gcc will link automatically for single files but you have to do it for multiple files. I tried using the -v option but it still errored out saying 'cannot find /tmp/string-48040e.o: No such file or directory'. – Edited Content Jan 29 '15 at 07:52
  • @EditedContent please edit your question to reflect what you are trying to do. – Michael Haidl Jan 29 '15 at 08:30
  • Just use something like "clang -o prog main.o file1.o file2.o ...". – Richard Pennington Jan 29 '15 at 11:51