VSCode IntelliSense proposes a .h file but marks it not found - c++

I can't compile a simple stupid demo "project" using visual Studio Code.
As I'm trying to enrich my knowledge portfolio, I trying to learn the basics manually, as to speak I try to compile code manually, without that the Visual Studio Enterprise IDE does all automatically. So I decided to use Visual Studio Code on Mac + clang++ for a simple demo project.
My problem is: I have a Workspace. In this workspace, I have some directories. One Of those directories called Functions and Headers contains again directories: debug, release, include, src. In the src directory, I have the functions.cpp file containing two dummy methods:
int add(int x, int y)
{
return x + y;
}
int subtract(int x, int y)
{
return x - y;
}
and in the main.cpp:
#include "functions.h"
int main()
{
int x = add(1, 3);
int y = subtract(5,1);
return 0;
}
In my include folder I have two method declarations:
int add(int x, int y);
int subtract(int x, int y);
The interesting point is: When I'm in main.cpp and I type #include " then IntelliSense finds the functions.h and proposes that file to me.
Also, the method calls are highlighted correctly. But: after including the file, Visual Studio Code marks the file read and says: functions.h file not found.
Is that a visual Studio Code bug? Because I also had a look on the settings and the settings seems ok (see picture).
Clang writes:
Ginos-MBP:src ginovalentinopensuni$ clang++ functions.cpp main.cpp main.cpp:2:10: fatal error: 'functions.h' file not found
#include "functions.h"
^~~~~~~~~~~~~ 1 error generated.
Do I miss something or am I doing something wrong? I also looked at the other similar questions here, but there was nothing helpful.
Pictures:

Indeed. The VSCode include path that allowed #include "functions.h" to be autocompleted has no bearing on what is a valid path to the compiler. There are two ways to address this:
Fix the path in the source file
Since you are including the header functions.h from the ./src directory, try instead writing:
#include "../include/functions.h"
int main()
{
int x = add(1, 3);
int y = subtract(5,1);
return 0;
}
This should work. Unless a specific include path is given to the compiler, clang++, like most other C++ compilers, will interpret the path relative to the location of the source file. In this case, it needs to go up a directory and then into the include directory in order to locate the functions.h header file.
Add an include path to that of clang++
From reading the clang++ man page, it can be ascertained that the -I flag may be used to specify an additional include path for the compiler to search to find header files. So, assuming that the command was run from the ./ directory, one jump out of the src directory and the root of the project:
clang++ src/main.cpp -I./include
would also address the issue at hand.

Related

My Class.cpp is not being seen by the linker

I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions.
main.cpp
#include "Fan.h"
int main() {
Fan fan1;
return 0;
}
Fan.h
#ifndef FAN_H
#define FAN_H
class Fan {
public:
Fan();
};
#endif
Fan.cpp
#include "Fan.h"
#include <iostream>
using namespace std;
Fan::Fan() {
cout << "Fan Class" << endl;
}
I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code.
If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right.
Would appreciate any help!
EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error:
[Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\<USER>\Desktop\Fan\"tempCodeRunnerFile
C:\Users\<USER>\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()'
collect2.exe: error: ld returned 1 exit status
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever).
There are two steps involved here:
cpp -> obj (compiling each source file into a matching object file)
obj -> exe (linking many object files into an executable)
I would say to make a CMakeLists.txt file and add main.cpp and fan.cpp into the add_executable section. Then VS can handle and run files through CMake.

Why my Visual Studio failed to find definition?

I'm trying to read some code(those code depends some unavailable lib so I'm not able to compile them, luckily I only need to read them) and using VS community 2017 15.7.5. I download the whole thing from github and use File->Open->Folder to import the whole folder. I found that when I try to find definition of a function(using F12), VS always jump to the declaration in the head file, even the .h file and the .cc containing the definition are in the same folder. What can I do to solve this problem?
Here's a simple demo:
main.cc:
#include "test.h"
int main(int argc, char *argv[])
{
foo();
}
test.h
void foo();
test.cc
void foo(){}
putting everything in one folder and open using operation above, no matter I use F12 or Ctrl+F12, VS only open test.h but not test.cc.
Instead of F12, try ctrl + F12. LINK: Default Keyboard Shortcuts in Visual Studio

Eclipse CDT Including Unreferenced Files in Automatically Generated Build

I'm new to cpp in eclipse and trying to mess with simple builds. I have made a basic project which automatically generates build info (no user-defined makefile).
Simple (Working) Case
I made a "Hello World" project called Test (not an empty project). It has one file - Test.cpp with a main() in => builds and runs fine.
Test.cpp
int main() {
// output some stuff
}
Slightly More Complex (Working) Case
I make a new file called Main.cpp. Move the main() function into Main.cpp and make a decleration of a function in main too - void test();
The test() function lives in Test.cpp, which is where I provide the function definition.
Main.cpp
#include <iostream>
void test();
int main() {
// Use test()
}
Test.cpp
#include <iostream>
void test() {
// output some stuff
}
Build it, there are now two .o files in the Debug directory - Test.o and Main.o. This runs fine.
The Problem
Now I try to introduce a third file - Limits.cpp.
Limits.cpp
#include <iostream>
void printLimits() {
// Print out limits for different integer sizes
}
Again I provide a deceleration of this function in Main.cpp.
Main.cpp
#include <iostream>
void test();
void printLimits();
int main() {
// Use printLimits()
}
This time there is no object file created for Limits in the Debug folder => the build fails.
Main.cpp:*line* undefined reference to `getLimits()'
It just looks like the autogenerated build config for the project is duff. I've tried looking around the project properties but I have had no luck. I've tried including the path/file in Include Paths/Include files, other objects, and I've checked just about every other property I can see. This has been frustrating me for two days.
The strange thing about this is case 2. It looks like because Test.cpp was the first file made it always includes this in the build? (even if it is not imported). Where as because Limits.cpp is new and not imported it doesn't generate an object file so it can't link the function.
I could me making an error by needing to include the files but my understanding is if all the object files make it to the linker then all will be well (ie I just need the function declarations when making the object files which is what I have here).
With a "Blank project" it seems to compile all the cpp files even if they are never used so my case above works. (Although, it doesn't work for files in subfolders of src). Looks like its a "feature" of the "Hello world" project type but it's going to drive me crazy knowing there must be a way to get it to do this.
If anyone knows if it is possible to include this file without writing my own makefile (I'm not a makefile guru (yet!)), or let me know if this is not possible with autogen CDT that would be great.
Using MinGW GCC toolchain.
Thanks

Header not refreshed - Visual C++ 2012

Using Visual C++ 2012, without precompiled headers:
When I change a header file that is included in multiple files, the change is not taken into account when building.
If I rebuild all, the change is taken into account.
Reproduction case:
include.h
#ifndef INCLUDE_H_
#define INCLUDE_H_
class A {
public:
A(int i) : i_(i) { }
int i_;
};
class B {
public:
B(int i = 1) : a_(i) { }
A a_;
};
#endif INCLUDE_H_
dummy.cpp
#include "include.h"
main.cpp
#include <iostream>
#include "include.h"
int main(int, char**) {
B b;
std::cout << b.a_.i_ << std::endl;
return 0;
}
This outputs 1.
Now I change int i = 1 to int i = 2 in include.h; I build and run, it outputs 1!
I rebuild and run, it outputs 2.
The dummy.cpp file is necessary to reproduce the error. In real-life, this file is using include.h but not classes A and B (but this doesn't seem to change anything; declaring a class C with members A and B in dummy.cpp will still reproduce the problem). When removing the file dummy.cpp, or renaming it to zdummy.cpp (presumably it will be compiled after main.cpp), then the problem disappears.
I tried with include guards, with pragma once, with both of them, with none of them, the problem is reproduced in each case.
I cannot reproduce this problem with Code::Blocks/GCC; I didn't try with older versions of Visual Studio.
Am I missing something or is this really a bug in Visual Studio? If the latter, is there a known workaround? (Other than re-building at every step)
The header file must be part of the project. If it isn't the project will still build i.e. the compiler can find it, but Visual Studio won't track the date of the file.
I've been through such thing in the past.
Do you use precompiled headers? If you do please remove any project specific headers from the PCH. That is a lame mistake. Only place external, nonchanging headersin PCH, like C/C++ standard headers, Windows headers, Boost etc.

Trying to use functions in a header/cpp file

I have two files:
hello.h and hello.cpp
hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
using namespace std;
void PrintMessage();
#endif
hello.cpp
#include <iostream>
#include "hello.h"
using namespace std;
void PrintMessage()
{
cout << "I want to be displayed!";
}
Now, I want to use PrintMessage() in a new .cpp file, but I keep getting an error message. This is what I'm doing:
printingmessage.cpp
#include <iostream>
#include "hello.h"
using namespace std;
int main()
{
PrintMessage();
return 0;
}
Am I just doing something blatantly wrong? I do have all of them in the same folder; I assume it has something to do with Dev-C++ (what I'm using to write/compile/run), but I can't figure it out. Anyone have any ideas?
I created a folder on my desktop, put all three files inside, and I tried to compile my printingmessage.cpp file exactly as it is. This is the error I'm getting:
[Linker error] Undefined reference to 'PrintMessage()'
i don't know dev C++ , but i would strongly advise if you do any serious coding to learn/move to the terminal and use make files, or a newer IDE such as visual studios.
heres a short script you can run save it as bash.sh
something like this
g++ hello.cpp -O2 -g -c
g++ hello.o printmessage.cpp -Wall -O2 -o print
then run it with ./print
I assume it has something to do with Dev-C++ (what I'm using to
write/compile/run), but I can't figure it out.
I guess so, too. Behind the scenes, the following things have to happen:
Both files get compiled. This creates a *.obj file for every *.cpp file, and uses the header.
The object files are linked against one another and possibly against required libraries.
Your problem lies in the “one another” part of the second step: the code compiles all right, but linking fails. The header file is irrelevant at that point. More precisely, the linker invocation for printingmessage.obj contains a reference to a function which isn't defined in that object file or any of the default libraries. Most likely the problem is due to the *.cpp files not being part of the same project. You need to create a multi-source-file project where you can link multiple object files. How you do that with Dev-C++ is probably somewhere in their manuals.