clang-format Visual Studio plugin and cmake - c++

I've installed the latest clang-format plugin for Visual Studio from http://llvm.org/builds/ and I'm trying to use it with a VS solution file that's generated from CMake. I've got the following directory structure:
./
├──project/
│ ├──.clang-format
│ └──source.cpp #all sourcefiles are here
└──build/ # cmake build files
When I open the cmake-generated .sln file and press CTRL+R, CTRL+F, no formatting happens. When I set Tools->Options->LLVM/Clang->ClangFormat->Fallback Style to anything (e.g. 'LLVM') then it formats (to the default style), but not if I set fallback to 'none' - which means my .clang-format file doesn't get loaded.
I tried copying the .clang-format file all over the project directories, i.e. putting it in the project dir, sub-directories, next to the source files, in the build directory, in the build/x64 directory, etc. - but it just doesn't get picked up.
Where do I have to put the file so that it gets picked up by the plugin?

I solved this, and it had nothing to do with where you put the .clang-format file. It's fine to just put it in the root of the source directory, as I did it.
Why it didn't format at all was that a .clang-format requires either the line BasedOnStyle: LLVM (or any other default style) or to define all required parameters.
If you just have a .clang-format file with a few lines, like for example:
---
ColumnLimit: '120'
IndentWidth: '4'
Language: Cpp
Standard: Cpp11
UseTab: Never
...
then it will not do any formatting whatsoever. Probably it can't work without a full set of rules, and in this case, it doesn't know to which default settings whatsoever it should fall back to.

Related

Why my "Reso"(resources) file is not recognized in Clion while trying to run OpenCV?

I'm trying to run a single program that opens a image file in Clion C++:
See image:
As you can see here, i have a file named "Reso" and a image named "test.png", but this code always will promt "Error reading image"
NOTE: I have proven that this does work in Visual Studio perfectly, but for some reason the file is not recognized in Clion
When you run a binary in CLion, your application will have by default the current working directory set to where your binary is located, which is probably somewhere in your cmake-build-debug directory.
In your case you expect a certain directory, because your resource file is somehwere else. You can either change your code to allow passing the path by argument, or you can set the working directory in CLion for your binary.
In the top you have this bar, where you can choose your build target. There is an option Edit configurations and when you open this and select your target, you can see a field Working directory. Set this field appropriate for your project.

Set Visual Studio Code path to .clang_format file

How do I tell vs code where to look for the .clang_format file? I have this file in location which is not the root of my vscode project.
Clang_format_path is apparently not what I need, because it specifies the path to the clang executable:
C_Cpp: Clang_format_path The full path of the clang-format executable.
If not specified, and clang-format is available in the environment
path, that is used. If not found in the environment path, a copy of
clang-format bundled with the extension will be used
Then there is Clang_format_style.
C_Cpp: Clang_format_style Coding style, currently supports: Visual Studio, LLVM, Google, Chromium, Mozilla, WebKit. Use "file" to load
the style from a .clang-format file in the current or parent
directory. Use {key: value, ...} to set specific parameters. For
example, the "Visual Studio" style is similar to: { BasedOnStyle:
LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces:
Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels:
false, ColumnLimit: 0, AccessModifierOffset: -4 }
I can set it to file but how do I specify where to look for the file?
Unfortunately, it seems that this is not possible at the moment. The extension look for the .clang-format file only in the project directory.
I was looking for the same, however the C/CPP extension by microsoft was unable to do that.
You need to download install Clang which comes bundled with the LLVM
https://releases.llvm.org/download.html
(Choose the one for your system under the Pre-Built Binaries)
While installing it will ask you to add LLVM to the path directory. If you dont want to add it manually then you have to add it to vscode settings.json later.
Install clang-format extension in VS code first -
https://marketplace.visualstudio.com/items?itemName=xaver.clang-format
After installation set clang-format as your default formatter
Place your .clang-format file in the opened project directory
Then head to your settings.json and add this line
"clang-format.language.cpp.style": "file"
(Skip this step if you have already added LLVM to path)
Inside settings.json add this line replacing the adress with your LLVM(clang) path
"clang-format.executable": "/absolute/path/to/clang-format"
Now, the extension will automatically search for the .clang-format file in the active directory.
You have to paste the .clang-format file everytime you change your active directories. There is no other way to define the path to .clang-format directly in the settings.
For more info on path setting see the answer here
How do I specify a clang-format file?

clang-format change one formating option in one file only

We have a custom .clang-format file in the root folder of our repository and use clang-format with -style=file for all C and C++ files.
I like to change the formatting rules of clang-format for one file only.
In particular I like to switch BinPackArguments form true to false.
I know, that I could place a .clang-format file in the corresponding folder. This option does effect all files in this folder.
Formatting it differently once is not an option, because our version control system will reject not formatted files.
I know, that I could turn off the formatter via the comment
// clang-format off
This has the drawback that the file is not formatted at all.
Is there a way to change one option for one file only (perhaps via a comment)?

How to feed Visual Studio Clang-Format plugin with clang-format file?

So I downloaded, installed, and inserted into path the clang formatting plugin. I also tested it and it works for Google (Mozilla, etc.) formatting options out of the box, yet I cannot get it working with my .clang-format file. (I've put my file into the same folder as my source file, changed its encoding into UTF-8, also tried to put it into clang install folder, add file into project, write its contents inside '{key:value}' yet formatting does not happen). So how do you feed formatting file to chrome-format extension?
My file contents:
{ BasedOnStyle: "LLVM", IndentWidth: 4 }
My file name:nm.clang-format
Go to Tools->Options->LLVM/Clang->ClangFormat and put file in the Style option field.
Then place your style file named .clang-format (this is the full filename, not an extension) either in the source file's directory or one of its parent directories. Windows Explorer won't let you create filenames with leading . so you need to go to the console for this.
If like me you got confused later on where the .clang-format was living, use procmon to track the file reads of clang-format.exe
For the record, it seems that if both "Fallback Style" and "Style" are set to "file", no formatting will happen even if the style file is at its correct location. Setting "Fallback Style" to something different than "file" (e.g. "none") helps.
In VS2019 works if the clang-format file is named as .clang-format.
It must be .clang-format, not .clang-format.txt or clang-format.txt.

Xcode 4 file input/output, command line tool C++

I'm trying to figure out where to save multiple .txt files so that i can have a command line tool project in Xcode read directly in from them while running it.
I understand that Xcode compiles everything to a folder, DerivedData, which i have saved in the same location as my source code for each project respectively.
can i save multiple .txt files anywhere in the DerivedData folder or include it in the build settings and phases so that when i run the command line tool i can type the name of a file, it will read in from that file.
By default the compiled/linked binary will look into its own directory for files.
For example, my binaries are at ProjectName/Build/Products/Debug/ and therefore it will look for files from that dir.
You can use relative path from that folder to the outside.
Or, you can create a symbolic link to another directory (on Terminal):
ln -s source_dir target_file
target_file must be located in the same directory as your binary. And you can reference the other files like "target_file/file1.txt", etc.