clang-format -style=file not working in Ubuntu 18.04 - c++

I'm on Ubuntu 18.04 with clang-format 9.
I've read the clang-format documentation where it says:
clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the .clang-format or _clang-format file in the project directory.
When using -style=file, clang-format for each input file will try to find the .clang-format file located in the closest parent directory of the input file. When the standard input is used, the search is started from the current directory.
No matter how I create the .clang_format file (I've tried with clang-format -style=google -dump-config > .clang_format) or where I put it, if I execute clang-format -style=file <file> it doesn't format anything.
Anyone that have the same issue?
For example, if I have a file hello.cpp:
#include <stdio>
std::string s=" VERY BAD"
"FORMATTING";
int main() {
std::cout<< "Hello world!"<<'\n';
return 0;
}
If I run:
$ clang-format -style=mozilla -dump-config > .clang_format
and even if I don't edit the .clang_format file, then
$ clang-format -style=file hello.cpp
I get the default LLVM formatting style instead of the Mozilla style:
#include <stdio>
std::string s = " VERY BAD"
"FORMATTING";
int main() {
std::cout << "Hello world!" << '\n';
return 0;
}
but if I run $ clang-format -style=mozilla hello.cpp then I get
#include <stdio>
std::string s = " VERY BAD"
"FORMATTING";
int
main()
{
std::cout << "Hello world!" << '\n';
return 0;
}
and the same happens if I move the previously generated .clang_format in the parent directory of the hello.cpp directory.
I've tried anything but it seems that I have to stick with preconfigured styles.
Anyone with the same problem?
Can I get some sort of logging from clang-format?
SOLUTION:
the name of the file must be .clang-format, not .clang_format!

I was creating a configuration file with a wrong name.
It must be named .clang-format, not .clang_format.

clang-format -i <file> is sufficient if you've placed your .clang-format file at the project root. The -i stands for in place. The commands you paste should have spit a formatted file out to standard output. The reason for this is that clang-format won't alter your file by default. Seems weird for a formatter at first, but it's a good safety precaution, in my opinion.

Related

VSCode ignores cpp standard when folder is opened in WSL: Ubuntu?

When I open a folder with WSL: Ubuntu and then try to build the project it seems to ignore the cpp standard set in VSCode's User settings. I can choose the standard I want by configuring a task.json file however I want to know how to change the default cpp standard for any folder opened in VSCode with WSL: Ubuntu. I have tried adding the following in my User settings in VSCode.
...
"C_Cpp.default.cppStandard": "c++17",
...
"code-runner.executorMap": {
...
"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
...
},
And yet when I build the following and execute the output I get 201402 indicating it is using c++14.
#include <iostream>
int main()
{
std::cout << __cplusplus << std::endl;
return 0;
}
Any suggestions on how to change the default c++ standard?
It seems that if I open the file in windows then it works fine as well so I don't know what the difference is here?

vs2008,vs2015 cannot read files: weird behaviour

I'm having a very weird behaviour in Windows 10 enviroment using both VS2008 and VS2015 when I try to read a file using ifstream.
I'm using the following simple code:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream ifs("C:\\Users\\dd\\Desktop\\test.txt");
if(ifs.is_open())
{
std::string line;
while(std::getline(ifs,line))
{
std::cout << line << "\n";
}
}
else
{
std::cout << std::strerror(errno) << "\n";
}
return 0;
}
Obviously the file exists.
When I try to read the file .txt I get:
Permission denied
I run Visual Studio as Administrator and I have the permission to read the file because I am able to open it using Notepad++.
If I change the file extension, for instance .test, I am able to read the file content correctly and everything works as expected.
I uninstalled and then reinstalled VS2008, VS2015 and all C++ Redistributable, but nothing changed. Any help is greatly appreciated!
Perhaps your file isn't named test.txt. Did you check that your file is really named test.txt and not test.txt.txt (with a hidden extension)?
Try to list files in command prompt to see what the real file name is:
cmd>
cd C:\Users\dd\Desktop\
dir | findstr test
Onother possibilities:
test.txt could possibly use similarly looking letters from another
language (in your code or in actual file name).
not properly quoted slashes in your code (e.g. C:\Users\\ instead of C:\\Users\\dd\\). Try to change to forward slashes.

Can t see the output of my c++ program (debian)

This might be a dumb question but...
I am programming some stuff in C++, it compile well on g++, but when I start the binary, there is nothing printed, even if I redirect the output in a file.
Example:
print.cpp
#include <iostream>
using namespace std;
/*...*/
int main ()
{
//Table tab;
//tab.set_all('_');
//tab.setc(1, 1, 'c');
//tab.setc(10, 5, 'd');
cout << "print" << endl;
//tab.print();
cout << "end" << endl;
return 0;
}
In shell:
>g++ print.cpp -o print
>print
>print > t
>cat t
>
Is it a problem in my code, or do I start my program in the wrong way?
By typing print in your shell you are executing the print command, which is a built-in of your shell that prints nothing without any arguments.
To launch your binary, type ./print. This solves the confusion between the print command and the binary print in the current directory.
If you just call
> print
you are actually executing /usr/bin/print, that from the man page is
NAME
run-mailcap, view, see, edit, compose, print - execute programs via entries in the mailcap file
Tu run your code you should do one of these three things:
If from the same directory
> ./print
From an other directory
> /path/to/exe/print
Add the directory where the exe live (/path/to/exe/) in the PATH before /usr/bin
> export PATH=/path/to/exe:$PATH
> print
If you want to add it permanently, just add export PATH=/path/to/exe:$PATH to you ~/.profile file
print is the name of a program from mailcap package. Typing print into the shell and hitting the Return key will execute it (from /usr/bin/print). Start your program by typing ./print.

Why would I get a syntax error near unexpected token? Command line arguments?

Here is the code I have- not sure why I am getting this error message:
$ ./main.cpp "hello" "is"
./main.cpp: line 4: syntax error near unexpected token `('
./main.cpp: line 4: `int main(int argc, char *argv[]){'
It compiles fine in g++, but when I run it, I get the above error. Any idea why? Here is my complete code..
#include <iostream>
#include <fstream>
int main(int argc, char *argv[]){
for(int i = 0; i < argc; i++){
std::cout << argc << " : " << argv[i] << '\n';
}
if (argc != 2){
std::cout << "\nUSAGE: 2 command line arguments please." << std::endl;
std::cout << "\n (1) Input file with raw event scores.\n (2) Output file to write into.";
}
// open the font file for reading
std::string in_file = argv[1];
std::ifstream istr(in_file.c_str());
if (!istr) {
std::cerr << "ERROR: Cannot open input file " << in_file << std::endl;
}
return 0;
}
You have to run the compiled program, not the source code:
$ g++ -o main main.cpp
$ ./main "hello" "is"
3 : ./main
3 : hello
3 : is
USAGE: 2 command line arguments please.
   (1) Input file with raw event scores.
   (2) Output file to write into.ERROR: Cannot open input file hello
Your example is trying to execute C++ code as a shell script, which isn't going to work. As you can see from the output of my test run of your program here, you still have some bugs to work out.
As both the other answers say, you're running it as a shell script, implicitly using /bin/sh.
The first two lines starting with # are treated by the shell as comments. The third line is blank, and does nothing. The fourth line is interpreted as a command int, but parentheses are special to the shell, and are not being use correctly here. There probably isn't an int command in your $PATH, but the shell doesn't get a chance to report that because it chokes on the syntax error.
None of these details are particularly important; the problem is that you're executing the program incorrectly. But it might be interesting to see why those specific error messages are printed.
And it appears that you've done something like chmod +x main.cpp; otherwise the shell would have refused to try to execute it in the first place. Making a C++ source file executable isn't going to cause any real harm (as long as it's readable and writable), but it's not at all useful, and as you've seen it delayed the detection of your error. If you do chmod -x main.cpp, and then try ./main.cpp again, you'll get a "Permission denied" error instead.
As Carl's answer says, you need to execute the executable file generated by the compiler, not the C++ source file. That's why there's a compiler. The compiler (well, actually the linker) will automatically do the equivalent of chmod +x on the executable file it generates.
The file command will tell you what kind of file something is, which affects what you can do with it. For example, using your code on my system, after running g++ main.cpp -o main:
$ file main.cpp
main.cpp: ASCII C program text
$ file main
main: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xacee0dfd9ded7aacefd679947e106b500c2cf75b, not stripped
$
(The file command should have recognized main.cpp as C++ rather than as C, but sometimes it guesses wrong.)
"ELF" is the executable format used by my system; the file contains executable machine code plus some other information. The pre-installed commands on the system use the same format.
The details may differ on your system -- and will differ substantially on non-Unix-like systems such as MS Windows. For example, on Windows executable files are normally named with a .exe extension.
The compiler, by default, creates an executable called "a.out", so you want to do:
$ a.out "hello" "is"
Typing "./main.cpp" is trying to execute the C++ source file, probably as a shell script

Scite: "Go" not running compiled and built code, giving "not recognized" error

The "Go" function is Scite is giving me the following error
"'.' is not recognized as an internal or external command,
operable program or batch file."
Is the '.' trying to relate to a path?
I have no trouble compiling and building. The exe file built works too. I just used a simple hello world code:
int main()
{
cout << "Hello " ;
return 0;
}
Thanks for any help.
This is because scite is using './' before the generated output file to execute the program and this is what we normally do to execute the programs in 'terminal' (linux). however, this is not required in windows and we just specify the output name and hit enter to execute '.exe' files.
you need to open cpp.properties in option menu and lookup the following:
# C++ styles
under the comment
# Braces are only matched in operator style
edit the line
command.go.*.c=./$(FileName)
to remove './'. Make it
command.go.*.c=$(FileName)
Again repeat the same thing below the following comment:
# To make the Go command both compile (if needed) and execute, use this setting:
#command.go.needs.*.c=gcc $(ccopts) -std=c99 $(FileNameExt) -o $(FileName)
Change
command.go.*.c=./$(FileName)
to
command.go.*.c=$(FileName)
'make' setting
If you are using mingW-gcc then lookup for the 'make' program in mingW-gcc installation folder. That should be 'mingw32-make'. Below the comment:
# Braces are only matched in operator style
Change
make.command=make
to
make.command=mingw32-make