G++ Compiling Error with .cpp - c++

I am having issues with compiling my .cpp file. There is nothing wrong with the code, and I suck at C++ and g++, so sorry if I suck at this. Anyways, I am getting the error message:
'Main.cpp -o Main
error: Main.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.`
Any idea as to why this might be happening?

Say you have file main.cpp inside of directory foo. To compile it with GCC, you navigate to foo and issue this command:
g++ main.cpp
This has to work. There's not much more to it.

The command
g++ -o Main Main.cpp
should create an executable named Main from your source files directly. The error message says that you either didn't specify any input files, or they can't be found in the current working directory (though I'd expect a message like error: Cannot open 'Main.cpp' then). Also be sure about the source filename spelling, when running the command from an OS supporting case sensitive filenames.
Saying
g++ Main.cpp
will create an executable file named a.out (which is the default name).

I fixed it. Some times what happens is when you are using VS Code and say you were on your previous project and you open a new tab and start working there , and save in some other directory than your previous project , in the terminal below you are in the same directory and thats the reason this error comes.
Just open a new window ,save and check the terminal whether you are in correct directory and you are good to go !
Happy Coding !

Related

C++ package cannot find header locations called from other header files in package

Before I start off, please bear with me, I'm very new to cpp with no formal training, and this question may have been asked and answered already, but I'm not sure what to call anything.
So I have a cpp program where I want to use this open source google sling package in. I have the google sling package in the same directory as my main.cpp program, and I can include one header with the path relative to the program, but that header(A) calls other headers(B:) within the package and the relative path to those headers(B:) is not relative to that header(A) file. Here is the error stack:
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ g++ -Isling main.cpp
In file included from main.cpp:7:0:
sling/frame/object.h:25:10: fatal error: sling/base/logging.h: No such file or directory
#include "sling/base/logging.h"
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ ls
bin obj test_project.cbp test_project.layout
main.cpp sling test_project.depend text_testfiles
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ vim main.cpp
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project$ cd sling/base
name#name-ThinkCentre-M83:~/Desktop/c++coding_projects/test_project/sling/base$ ls
bitcast.h flags.cc libinit.cc port.h status.cc types.h
BUILD flags.h logging.cc registry.cc status.h
clock.cc init.cc logging.h registry.h strtoint.cc
clock.h init.h macros.h slice.h strtoint.h
So sling/base/logging.h is actually there, but since it's being called from sling/frame/object.h, the correct relative path would be ../base/logging.h(at least my limited knowledge tells me so). I think I have to set it up so that it's part of the global path that my cpp compiler searches in for dependencies. Either way I've done something terribly wrong.
I'm looking for a semi quick fix, but also I'd like to avoid this in the future, so a link to the appropriate information would be very much appreciated as well.
Edit:
Also tried with same error:
g++ -I.sling main.cpp
g++ -Itest_project main.cpp
g++ -isystem sling main.cpp
g++ -iwithprefixbefore "/home/.../test_project/" main.cpp where ... is the path from home to my test project
Thank you melpomene, the correct answer was g++ -I. main.cpp. Case closed.
Thanks to Jerry Jeremiah in the comments for this info:
The #include <x> says "include the file from the include path" and the #include "x" says "include the file from the current directory"
Thus, my program was calling the header file "sling/base/logger.h" from the directory "sling/frame/" (which is where the originally called "sling/frame/object.h" lives) and for obvious reasons couldn't find it there.

Fatal error: no such file or directory

I have downloaded the source code to do some benchmark for couple of C++ library. I don't have much knowledge in C++. I downloaded the libraries. When I try to compile the code
g++ test_matrix_pseudoinverse.cpp -o test_matrix_pseudoinverse
I get this error
test_matrix_pseudoinverse.cpp:26:64: fatal error:
opencv2.4/modules/core/include/opencv2/core/core.hpp: No such file or
directory #include
^ compilation terminated.
The source code with all the libraries are in github
https://github.com/shiblybcc/linear-algebra-benchmark
I have downloaded the code from here. It would be nice if someone could tell me why I am getting this error and how to run the code without any error.
I am on linux and I have added opencv2 folder in /usr/local/include directory.
I guess this is resulted from not setting the include path if the file complained by the compiler does exist. Suppose your OpenCV source code tree is under /home/yourhome (i.e the full path to the file reported not existed should be /home/yourhome/opencv2.4/modules/core/include/opencv2/core/core.hpp), you need to specify the include path by -I/home/yourhome argument. Therefore the full command line to build should be as:
g++ -I/home/yourhome test_matrix_pseudoinverse.cpp -o test_matrix_pseudoinverse

c++ compiler (g++) strange behaviour (no such file or directory)

I am working in eclipse with MinGw (g++) compiler.
So my problem is when I import .h file from library I have downloaded and I try to build(compile) my project, error is "no such file or directory" for that .h file you can see on picture but still the class from that header file is recognised in the code!
Another strange thing is if I make intentional error in that .h file #import is succesfull and the error from that .h file is shown, that means it trys to compile that .h file.
So it does not know where the file is but it still compiles it ??? what???
cmd line:
g++ -Ic:D:\Documents\cpp_testing\bignum_testing\lib Main.cpp
error:
Main.cpp:10:22: fatal error: Cbignums.h: No such file or directory
#include "Cbignums.h"
^
compilation terminated.
I hope somone will know how to fix this and that it will help other people!
Picture without error in .h file:
Picture with intentional error in .h file!
I suspect that you should
#include "lib/Cbignums.h"
in bignum_testing.cpp if that is the path of the Cbignums.h - relative to the source file.
The IDE recognizes the type because you have the header in your project but the red lines indicate that the include will fail.
EOF is mostly a C thing from <cstdio> (or <stdio.h> form the C standard library, not the C++ one) which you probably forgot to include (e.g. by commenting its //#include <cstdio>)
Try compiling with appropriate -I and -H preprocessor options to g++ (of course, take time to read the documentation to find what they are doing).
BTW, you could ask for the preprocessed form with g++ -C -E Cbignums.cpp > Cbignums.ii then look, with some editor or pager, into the generated Cbignums.ii file.
I strongly recommend you to read a good book about Programming using C++ and to read the GCC documentation. In general, read the documentation of everything you are using for development (tools, e.g. compilers, and libraries)
PS. Every free software C++ compiler I know (GCC & Clang/LLVM...) are command line tools, so run them in a terminal, perhaps thru GNU make. Notice that Eclipse is not a compiler (it is an editor, self-glorified as an IDE), and you probably are not using it cleverly. Don't forget to pass -Wall -g to g++

Using gcov to test a c++ program

I am using gcov for the first time to analyze my program (C++)
The program consists of three classes and I have built the project using Code::Blocks.
When I am invoking the program using the following command:
C:\Users\XXX\Documents\Test\TreeObjModel\src>gcc
-fprofile-arcs -ftest-coverage Tree.cpp
I receive the following error:
Tree.cpp:1:18: fatal error: Tree.h: No such file or directory
compilation terminated
While the cpp files are in the directory "C:\Users\XXX\Documents\Test\TreeObjModel\src\" , the header files are in directory "C:\Users\XXX\Documents\Test\TreeObjModel\include\"
Do we need to have both the code and header files in the same directory?
Thanks in advance.
You should use the -I flag to specify where your header files are.
Judging from your example, you should add -I../include
You have at least two options to instruct the compiler where to find the header files (includes).
-Ipath_to_includes as parameter for gcc compiler. E.g. -I../include
When including in your program, specify the directory. E.g. #include "../include/foo.h"
My strategy would be to just compile my project successfully and only then try to use some other stuff, like flags for code coverage. I say this because your error does not have anything to do with gcov, and trying to instrument your program to get code coverage before your program even compiles, makes things more complicated for you. One step at a time ;)

How to use gdb on c++ header files?

I tried to search this question online, but it seems that I can't find a good solution for my problem. Well, I'm trying to use gdb to debug my c++ program. And the program is made up of a simple main.cpp and a model.h. And the compiling command is
g++ -Wall -g -c main.cpp
g++ -Wall -g main.o -o OUTPUT
As almost all the algorithm is stored in model.h, I need to debug that header file rather than the cpp file. However, whenever I tried to place a break point on the header like
tbreak model.h:163
gdb always give me a message that"No source file named TNFmodel.h".
In another question breakpoints in GDB, I saw a solution by adding the folder that containing the header into the library by "dir". But my header file is already in source folder, and after trying
dir ./
The problem maintains.
So anybody know what's wrong? How to use gdb to debug a header file?
As suggested by https://stackoverflow.com/users/760746/nobody, one way to make sure the header to be in the sources is to veryfy it by checking
info sources
After ensuring the header itself be in the sources(in my case, the problem is that the case of a letter in the header name was mixed up, and somehow it went through the compiling on my mac book), inserting breakpoint in lines of a header file works just fine.
Try to use break with your class/method name like this:
break class::method
What I've found is that is that the file names are sometimes shortened. Using info sources I was able to find the shortened name that GCC used. When I set the breakpoint using the shortened file name, GDB correctly set the breakpoint.
For example the file CommonLibrary\headers\Endian.h was changed to COM~2\headers\Endian.h
This on Windows 10, running mingw-64.