# Using VIM with Clang



## moesasji (Feb 27, 2011)

I'm trying go get VIM to compile c++ code with Clang instead of gcc (as I prefer the debug-output from CLang). Unfortunately I am unable to get :make to use cLang 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/llvm/trunk/utils/vim/vimrc

*) the above should have worked according to the discussion in the llvm mailing-list here: http://markmail.org/message/mq73uxp...man+page:1+mid:ttziiaqgt2oabua6+state:results

Unfortunately typing *:make "program-name"* still uses gcc 

4) After that I've tried setting *:compiler clang* in vim after installing the following script: http://www.vim.org/scripts/script.php?script_id=3259 
(typing *:scriptnames* confirms that this file does indeed get read; unfortunately VIM keeps using gcc) :q

After this I'm out of ideas, so hopefully someone can point me in the correct direction as google unfortunately doesn't.


----------



## kpedersen (Feb 28, 2011)

*:make* runs *make* surely?

So in your project Makefile make sure that CC=clang.

If *:make* is not actually running your Makefile but is rather using some sort of default common makefile, then perhaps try setting CC in the environment variables.


```
$ export CC=clang
$ vim main.cpp
```


----------



## moesasji (Feb 28, 2011)

kpedersen said:
			
		

> *:make* runs *make* surely?
> 
> So in your project Makefile make sure that CC=clang.
> 
> ...



You are indeed partly right, although the correct way to set which compiler runs is by typing something like *:set makeprg=clang++* for c++ code (see for example: http://www.justlinux.com/nhf/Programming/Introduction_to_C_Programming.html) as VIM ignores the $CC variable you mention.

This does improve things a bit as it now tries to compile using clang. Unfortunately it still does not work as 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 now 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)
```

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 some further thoughts?

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++*). Hopefully somebody knows what the correct way to do this is.


----------



## moesasji (Mar 6, 2011)

For those finding this topic while searching. My problem was caused by not setting the compiler correctly. The correct commands to compile with clang (for c++) is as follows.


```
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 Although the behavior still differs form compiling with gcc it at least compiles now.


----------

