Using VIM with Clang

1,093 views
Skip to first unread message

Moesasji

unread,
Mar 6, 2011, 4:19:58 AM3/6/11
to vim_use
I'm trying go get VIM to compile c or c++ code with Clang instead of
gcc (as I prefer the debug-output from CLang). Unfortunately I am
unable to get :make to use ClLang for compiling. So time to ask if
someone can point me in the right direction as I'm still getting to
grips with VIM:

Relevant info:
0) This is on FreeBSD stable with clang-develop from ports.
1) I've checked that compiling with clang++ from the CMD-line works as
it should.
2) Hence clang and clang++ are indeed in $PATH.
3) I've used the vimrc file as provided by the llvm project, see
https://llvm.org/svn/llvm-project/ll...tils/vim/vimrc

*) the above should have worked according to the discussion in the
llvm mailing-list here: http://markmail.org/message/mq73uxpc...+state:results
which it doesn't as it still uses gcc to compile.

4) I have subsequently set which compiler runs by typing :set
makeprg=clang++ for c++ code (I've also tried :set makeprg=clang
instead)

It clearly is using clang after this. Unfortunately it I get a strange
error-message that doesn't happen when compiling the same hello world
test-code directly from the command line. The output I get is:
---
hello:(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/crtbegin.o:(.data+0x0): first defined here
hello: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/crti.o:/usr/src/lib/csu/amd64/crti.S:(.init+0x0): first
defined here
hello:(.data+0x0): multiple definition of `__progname'
/usr/lib/crt1.o:(.data+0x0): first defined here
hello: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/crt1.o:crt1.c:(.text+0x0): first defined here
hello: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/crti.o:/usr/src/lib/csu/amd64/crti.S:(.fini+0x0): first
defined here
/usr/local/bin/ld: error in hello(.eh_frame); no .eh_frame_hdr table
will be created.
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
---
Which is the point at which I'm stuck (and google doesn't help me).
I've also tried with a piece of plain c-code instead of cpp, but this
leads to the same error-message. Hopefully this error-message triggers
thoughts on what I'm doing wrong here.

For clarity both the c and cpp-code compile fine with clang directly
from the commandline. So the problem must be in the way how I call
clang from vim (using :set makeprg=clang++ or :set makeprg=clang).
Hopefully somebody knows what the correct way to do this?

Ben Schmidt

unread,
Mar 6, 2011, 6:03:52 AM3/6/11
to vim...@googlegroups.com, Moesasji
> Relevant info:
> 0) This is on FreeBSD stable with clang-develop from ports.
> 1) I've checked that compiling with clang++ from the CMD-line works as
> it should.
> 2) Hence clang and clang++ are indeed in $PATH.

Are you using vim from the comandline/in the terminal, or are you using
gvim? If you're using gvim, perhaps the environment is different and
causing problems.

> 3) I've used the vimrc file as provided by the llvm project, see
> https://llvm.org/svn/llvm-project/ll...tils/vim/vimrc

URLs with '...' in them are incredibly unhelpful if anybody wants to
look up what you've referenced!

It looks to me like you are trying to link an existing executable
(hello), rather than compiling the source code (hello.c) or linking an
object file (hello.o), and so the linker is finding two copies of all
the system stuff: one copy that is in the executable from the last time
it was linked, and one copy from its attempt to compile it in now.
That's my best guess as to what's happening here, but even that's a
pretty long shot, as usually a linker won't do this, I don't think.

> For clarity both the c and cpp-code compile fine with clang directly
> from the commandline. So the problem must be in the way how I call
> clang from vim (using :set makeprg=clang++ or :set makeprg=clang).
> Hopefully somebody knows what the correct way to do this?

It will be easier to know what's going on and pinpoint the problem and
direct you in the right direction if you could fix the links to the
stuff you are using (or if it's short, include it in the body of your
email), and tell us exactly what commands you are running in Vim, and
how you are starting Vim.

Note also, perhaps it would be smarter to use a makefile, that can
invoke the compiler you want, but not require any change of 'makeprg' in
Vim, only possibly :compiler to correctly interpret the messages.

Cheers,

Ben.

miyamoto moesasji

unread,
Mar 6, 2011, 7:23:06 AM3/6/11
to vim...@googlegroups.com
Thanks for the swift reply Ben, to answer your questions and comment
on some of the points you raised

> Are you using vim from the comandline/in the terminal, or are you using
> gvim? If you're using gvim

This is with vim in the commandline, but the same happens with gvim.

> URLs with '...' in them are incredibly unhelpful if anybody wants to
> look up what you've referenced!

Apologies...copy-paste gone wrong:

1) the vimrc I used:
https://llvm.org/svn/llvm-project/llvm/trunk/utils/vim/vimrc
2) post to the llvm-mailing list:
http://markmail.org/message/mq73uxpc7cwpji2k#query:llvm%20vim%20gohman+page:1+mid:ttziiaqgt2oabua6+state:results

> It will be easier to know what's going on and pinpoint the problem and
> direct you in the right direction if you could fix the links to the
> stuff you are using (or if it's short, include it in the body of your
> email), and tell us exactly what commands you are running in Vim, and
> how you are starting Vim.

The fixed links are above, and the commands I used (with a hello world
bit of code):
vim hello.cpp
:set makeprg=clang
:make hello.cpp

for gcc opening the file and typing :make hello works (but not for clang)

>
> Note also, perhaps it would be smarter to use a makefile, that can
> invoke the compiler you want, but not require any change of 'makeprg' in
> Vim, only possibly :compiler to correctly interpret the messages.

I have not yet tried this with a makefile, as I followed the approach
outlined under quickfix here:
http://www.justlinux.com/nhf/Programming/Introduction_to_C_Programming.html
Anyway I would hope that for such as simple compile this could work
with clang without having to figure out makefiles.

miyamoto moesasji

unread,
Mar 6, 2011, 8:10:43 AM3/6/11
to vim...@googlegroups.com
Seeing the list of commands made me wonder where I did set the
compiler....which is something I've should have spotted earlier as
that is what the problem was. Essentially typing:

vim hello.cpp
:compiler clang
:set makeprg=clang++
:make hello.cpp

while putting the following script in the compiler directory:
http://www.vim.org/scripts/script.php?script_id=3259 works as expected
(although the behavior still differs form compiling with gcc).

I feel stupid for not spotting this sooner. :-( Sorry for the noise!

Reply all
Reply to author
Forward
0 new messages