I have written the body of the function in the header file and so do not have a source file. when I tried running my project in visual studio .. I got an
error: Cannot open source file: No such file or directory.
How do I make visual studio understand that the definitions of the function are within the header itself?
You need to create a dummy source.cpp file just containing #include "source.h"
edit - I just tried this - Visual studio will let you do.
test.cpp
#include "test.h"
where test.h
#include "stdio.h"
int main()
{
printf("hello world");
return 0;
}
Interesting - but pointless !
Related
Anyone know how to rename a header file in vscode so that all includes of the header file will be updated accordingly?
For example, if I rename Header0.h to Header1.h, all includes will change from
#include "Header0.h"
to
#include "Header1.h"
Use of extensions is also welcome
I have not used C++ in visual studio in a lonnng lonnng time so probably this is a basic question but I would like some help.
I am opening someone else C++ code and the first thing I notice is that it is not recognizing its includes.
For example I have a main.cpp that has
#include "stdafx.h"
#include "myheader.h"
this cpp file is inside a "source" folder and those header files are under a "headers" folder.
I got an error "cannot open source file "stdafx.h" and "cannot open source file "myheader.h"
It is been ages I haven't touched C++ in visual studio. What configuration I should do to fix this?
if source and headers folders are in same level in folder structure you can use
#include "..\headers\myheader.h"
Similarly locate your stdafx.h and add the relative path to the .cpp file.
Why in Visual Studio 2017 the compiler will complain (LinkError 2019, seems to not compile the implementation) if I include my headers and implementation files rather than use the default pch.h and pch.cpp files? Obviously all the files mentioned are in the same folder.
(Note: Copy-Paste the same code in pch.h and pch.cpp will work fine)
Im using this sintax on header.h:
#pragma once
//init code
Implementation.cpp:
#include "header.h"
//impl code
Main.cpp:
#include "header.h"
//main code
I'm a beginner working on a software project in C using Eclipse.
We have numerous header and source files in the directory of the project (which is in the Eclipse workspace). Most of them are .c files, though some are .cpp.
I encounter a problem while trying to use, in a .cpp file, functions which are defined in a .c file. Usually adding the line "#include filename.h" allows usage of functions defined in filename.c, where both those files are in the same folder as the current one. This time, however, the current file is .cpp and Eclipse throws an "Unidentified reference" error (I am using an extern "C" block).
I've understood from similar Q&As that it's a linker problem, yet none of those referred to the simple case in which all files are in the same folder.
Any help will be greatly appreciated.
Code:
// SPFeatureExtract.cpp
#include <cstdlib>
#include <cstdio>
extern "C" {
#include "SPConfig.h"
}
foo() {
...
numOfImages = spConfigGetNumOfImages(config, msg); // *Unidentified reference error
}
// SPConfig.c
int spConfigGetNumOfImages(const SPConfig config, SP_CONFIG_MSG* msg) {
[implementation]
}
// SPConfig.h
int spConfigGetNumOfImages(const SPConfig config, SP_CONFIG_MSG* msg);
Error message:
E:\SoftProj\WorkSpace\Final\Debug/../SPFeatureExtract.cpp:58: undefined reference to `spConfigGetNumOfImages(sp_config_t*, sp_config_msg_t*)'
I am new to programming C++ and am trying to learn myself through websites (learncpp.com) although I am already stuck on compiling my first program =( . They use Visual Studio to program their code and because I am using a macbook, I just use vi and terminal (or should I use something else?)
Here's the helloworld.cpp program I wrote based on the tutorial:
#include "stdafx.h"
#include <iostream>
{
std::cout <<"Hello World!" <<std::end1;
return 0;
}
when I compiled (gcc -Wall hello.cpp) I get the error :
helloworld.cpp:1:10: fatal error: 'stdafx.h' file not found
#include "stdafx.h"
^
1 error generated.
Can anyone give me insight on to how to fix this?
stdafx.h is the precompiled header used by Visual Studio, you do not need this.
You seem to have missed out the int main() function
It is std::endl not std::end1
So something like this:
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
stdafx.h is a Precompiled Header file and it is specific to the Visual Studio.
Precompiled Header file is worthless unless you are facing slow compilation Time. In your program, you don't need them at all, so you can remove that and everything will be fine.
You might be guessing if it is not needed then why we include them?
I will Explain it:
Whenever we add header files (#include), The compiler will walk through it, Examine it and then compile the header file whenever CPP file is compiled.
This process is repeated for each and every CPP file that has header file included.
In case if you have 1000 CPP files in a project which has to say xyz.h header file included then the compiler will compile xyz.h file 1000 times. it may take a noticeable time.
To avoid that compiler gives us the option to "precompile" the header file so it will get compiled only once to speed up the compilation time.
Two problems:
a) stdafx.h is not needed (as others noted).
b) 'end1' should be 'endl' (note the letter 'l' vs. the number '1').