When I want to run the code the compiler says undefined reference to math::calc
I read questions and answers about this problem at StackOverflow and it do not help me solve my problem.
Comp.h
#include <utility>
namespace math {
typedef std::pair<double,double> Comp;
double getFirst(Comp a);
...
}
Comp.cpp
#include "comp.h"
namespace math {
double getFirst(Comp a) {
return a.first;
}
...
}
Comp file: every function return Comp or double. I call function from cal.cpp file several times
cal.h
#include "comp.h"
#include "helper.h"
namespace math {
Comp calc(const string& str);
...
}
cal.cpp
#include "eval.h"
namespace math {
Comp calc(const string& str) {
...
}
}
Cal file: Some functions return with comp type, not just the cal function.
helper.h
namespace math {
...
}
helper.cpp
#include "helper.h"
namespace math {
...
}
helper file just contains few function that I calling from cal.cpp file. Each function is calling several times.
main.cpp
#include "calc.h"
int main() {
string str = " ";
pair<double,double> res = math::calc(str);
cout << res.first << " " << res.second << endl;
return 0;
}
In the whole project, I do not use any classes.
I included every file that I calling except the c++ original file.
I use the std namespace in each file but I do not write it here.
I absolutely no idea what could be the problem with my code.
If I also include cal.cpp in the main.cpp the code editor says undefined reference to each file that I calling from helper.h. I do not want to include cal.cpp in the main file I just mentioned it
You have to compile all your project's *.cpp files and then link them (results of compilation) properly - the linking process depends on the environment/IDE you're using, just look it up. If you're not linking it properly the final executable wont have all the functions definitions that requires, hence getting undefined reference.
Related
I have the following files in a testing project:
Test.cpp:
#include "pch.h"
#include "CppUnitTest.h"
#include <iostream>
#include "PrintOne.cpp"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace PointandVectorCreationTest
{
TEST_CLASS(PointandVectorCreationTest)
{
public:
TEST_METHOD(TestMethod1)
{
std::string expected = "1\n";
std::stringstream buffer;
std::streambuf* sbuf = std::cout.rdbuf(); // Save cout's buffer
std::cout.rdbuf(buffer.rdbuf()); // Redirect cout to the stringstream buffer
int result = printOne();
// When finished, redirect cout to the original buffer
std::cout.rdbuf(sbuf);
std::cout << "std original buffer: \n";
std::cout << buffer.get();
// Test
Assert::AreEqual(expected, buffer.str());
}
};
}
PrintOne.cpp:
#include <iostream>
int printOne() {
std::cout << 1 << std::endl;
return 0;
}
When I try to run this test in Visual Studio, the linker throws the following error:
Error LNK2005 "int __cdecl printOne(void)" (?printOne##YAHXZ) already defined in PrintOne.obj
This error is thrown when linking Test.obj.
I'm not defining printOne anywhere in Test.cpp. In fact, if I just copy the definition of the function to Test.cpp and eliminate the PrintOne.cpp file like so:
Test.cpp:
#include "pch.h"
#include "CppUnitTest.h"
#include <iostream>
int printOne() {
std::cout << 1 << std::endl;
return 0;
}
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace PointandVectorCreationTest
{
TEST_CLASS(PointandVectorCreationTest)
{
public:
TEST_METHOD(TestMethod1)
{
std::string expected = "1\n";
std::stringstream buffer;
std::streambuf* sbuf = std::cout.rdbuf(); // Save cout's buffer
std::cout.rdbuf(buffer.rdbuf()); // Redirect cout to the stringstream buffer
int result = printOne();
// When finished, redirect cout to the original buffer
std::cout.rdbuf(sbuf);
std::cout << "std original buffer: \n";
std::cout << buffer.get();
// Test
Assert::AreEqual(expected, buffer.str());
}
};
}
The test runs just fine. I'd rather avoid writing all the functions I use in the same file I test them, so to me this shouldn't be a solution.
Why does the linker throw this exception? How can I fix this so I can keep my function definitions separate from the testing file?
I'm not defining printOne anywhere in Test.cpp.
Actually, you are, when you #include the source code of PrintOne.cpp into Test.cpp. If you then compile and link both Test.cpp and PrintOne.cpp together, the linker indeed sees 2 definitions of printOne(), one in each .obj file.
For what you are attempting to do, you need to add a .h file that just declares printOne(), and then you can #include that file into both .cpp files, where only one of them defines printOne(), eg:
Test.cpp:
#include "pch.h"
#include "CppUnitTest.h"
#include <iostream>
#include "PrintOne.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace PointandVectorCreationTest
{
TEST_CLASS(PointandVectorCreationTest)
{
public:
TEST_METHOD(TestMethod1)
{
std::string expected = "1\n";
std::stringstream buffer;
std::streambuf* sbuf = std::cout.rdbuf(); // Save cout's buffer
std::cout.rdbuf(buffer.rdbuf()); // Redirect cout to the stringstream buffer
int result = printOne();
// When finished, redirect cout to the original buffer
std::cout.rdbuf(sbuf);
std::cout << "std original buffer: \n";
std::cout << buffer.get();
// Test
Assert::AreEqual(expected, buffer.str());
}
};
}
PrintOne.h
#pragma once
int printOne();
PrintOne.cpp:
#include "PrintOne.h"
#include <iostream>
int printOne() {
std::cout << 1 << std::endl;
return 0;
}
When you have a line like #include "PrintOne.cpp" in your Test.cpp file, then the entire contents of PrintOne.cpp (including the definition of the printOne() function) will be included in Test.cpp's source code; thus, the Test.obj file will contain a definition of the printOne() function.
If you then also have the PrintOne.cpp file included in your project - as it seems from the fact that the linker is referencing a PrintOne.obj module - then there will also be a definition of the printOne() function in that object file, too.
So, you have multiple (though identical) definitions of the function, as signalled by the linker.
To avoid this, replace the inclusion of the PrintOne.cpp source file with one for a header (say #include "PrintOne.h") that provides a declaration (prototype/signature) for the printOne function (The contents of the source file will be added, anyway, if it is part of the project.)
Generally, it is not advisable to #include ... source (cpp) files (though there are some circumstances where it can be useful). Worth reading: When do I need to #include .cpp files?
I recently began learning C++. As a programmer coming from Python, I've noticed some general similarities when it comes to how certain things in C++ do the same thing over in Python.
One question I had is understanding Preprocessor directives. I/O Stream seems to be a common one to use in beginner programs.
Is #include effectively the same thing as import in Python, or is it completely different than importing "modules"?
C++ did not have modules until the latest standard (C++20). #include is not the same as import in the languages that support modules. Instead, it is a source code - level inclusion of a "header" file. Usually, the headers only contain declarations but not definitions of what you are "importing". The definitions are contained in compiled libraries that are added by the linker.
Congrats on diving in to C++, you're going to have many more questions and confusions coming from Python, especially if you use some of the newer standards (like C++11/14/17/20).
That aside, answering your question directly:
Is #include effectively the same thing as import in Python or is it completely different than importing "modules."
I won't speak to C++20 modules as that functionality is not fully supported across the various compilers and that is not your question. Unfortunately the answer is not a simple yes or no, it's kind of both.
In C and C++, the #include pre-processor directive essentially does a "copy-paste" of whatever file you #include before it does the compilation stage. This allows you to separate large chunks of code into easier to manage files and still reference the code in said file.
In Python/C#/Java and various other languages, you don't #include a file you want to access the classes and functions of, you import the namespace or module you wish to reference and the JIT compiler "knows" which file that module or namespace is in, allowing you to use the functionality of the code in that file.
Python and C++ don't "build" the code in the same way and thus don't reference parts of the source code in the same way.
To illustrate this point more succinctly, take the following C++ code:
file: fun.hpp
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
file: main.cpp
#include <iostream>
#include "fun.hpp"
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
In the above code, when we #include "fun.hpp", what the C++ pre-processor does before compiling is essentially "copy-and-paste" the code in iostream and fun.hpp, so what actually gets compiled is something like the following:
file: main.cpp
// #include <iostream> <- this is replaced with the whole std::iostream file
// not putting that here as it's huge.
// #include "fun.hpp" <- this is replaced with this:
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
It is because of this "copy-paste" that you also need to have include guards, because if you did something like the following:
file: main.cpp
#include <iostream>
#include "fun.hpp"
#include "fun.hpp"
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
This code won't compile because you'll get errors about various items being redeclared since what gets compiled is the following:
file: main.cpp
// #include <iostream> <- this is replaced with the whole std::iostream file
// not putting that here as it's huge.
// #include "fun.hpp" <- this is replaced with this:
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
// #include "fun.hpp" <- this is replaced with this:
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
int main(int argc, char* argvp[])
{
if (fun::get_fun() == FUN_NUM) {
std::cout << "Fun!" << std::endl;
}
return FUN_NUM;
}
To protect from the double inclusion and redefinition, you can simply do something like the following:
file: fun.hpp
#if !defined(FUN_HPP)
#define FUN_HPP
#define FUN_NUM 1
namespace fun
{
int get_fun()
{
return FUN_NUM;
}
}
#endif // define FUN_HPP
So unless you pass FUN_HPP as a pre-processor define to the compiler, then FUN_HPP will not be defined until the file is #include'd once, then any other times it's included, FUN_HPP will already be defined and thus the pre-processor will not include the code again, ridding the problem of double-definitions.
So where your question is concerned, the #include directive in C++ is somewhat like the import directive in Python, but mostly to the effect that they both allow the file you are putting that directive in, to access code more directly from that import or #include.
I hope that can add a little clarity.
All I'm trying to do is create a separate class to hold my Hello World function (this is for a class), but I am getting an "identifier is undefined" compiler error. What is the issue?
Here is my main function (helloworld.cpp):
#include <iostream>
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
And here is the header class (helloworld.h) :
#include <iostream>
void print_me() {
std::cout << "Hello World\n";
}
You have not included helloworld.h in helloworld.cpp. The following code should work:
#include <iostream>
#include "helloworld.h"
using namespace std;
int main() {
print_me();
system("pause");
return 0;
}
One thing to remember is from your compiler's point of view, there is no connection between the two files unless you specify it. The fact that both files have the same name does not have any significance for compiler.
Side note 1: Consider using include guards in your header files. For simple projects, it may not be obviously necessary but for larger projects, not using them can lead to annoying ambiguous compilation errors.
Side note 2: Implementing function bodies in header files is generally discouraged.
I'm working on making a game in C++. I have declared a Constant namespace used for global values that I need access to throughout the program. In there I have an ofstream for debugging purposes (yeah, I know it's not "constant" but it fits best there), which outputs only when it feels like it. I was able to make a small program demonstrating the problem. I apologize for it being spread across 4 files, but it is important, I promise.
main.cpp:
// Include necessary files
#include "test.h"
#include "constants.h"
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
// Start of program
Constant::outstream.open("test.txt");
// ...
// Do stuff
// Output debugging info
Test test;
test.print("Test", Constant::outstream);
// ...
// Do other stuff
// End of program
Constant::outstream.close();
return 0;
}
constants.h:
#ifndef _CONSTANTS_H
#define _CONSTANTS_H
#include <fstream>
namespace Constant
{
static ofstream outstream;
}
#endif
test.h:
#ifndef _TEST_H
#define _TEST_H
#include <string>
#include <fstream>
#include "constants.h"
class Test
{
public:
void print(string str, ofstream& out);
};
#endif
test.cpp:
#include "test.h"
using namespace std;
void Test::print(string str, ofstream& out)
{
out << "out: " << str << endl << flush; // Works
Constant::outstream << "Constant::outstream: " << str << endl << flush; // Doesn't
}
In the test.cpp file, the out << ... line works as it should, while the Constant::outsream << ... line doesn't do anything even though I'm passing Constant::outstream as the out parameter! I don't see any reason why these two lines should be in any way different.
Before posting this, I tried putting test.cpp's code in test.h, just to have less files for the question, and was amazed to see it work. If I copy-paste the Test::print() function into test.h (whether inside or out of the class Test { ... }), then both output commands work correctly. the problem only occurs if Test::print()'s implementation is in a separate file.
It seems like any references to Constant::outstream simply don't work in class cpp files (no compile error, just nothing happens). It works in main.cpp and in class header files, but any class cpp file it seems not to. Unfortunately, this is a big program I'm writing so pretty much every class has its own cpp implementation file, and that's really the one place I need to use this ofstream. Does anyone know the reason for this?
Thanks in advance,
Doug
Constant::outstream has internal linkage, thus a separate instance is created for each translation unit. In short, Constant::outstream in test.cpp and main.cpp are two different variables.
§3.5.2 A name having namespace scope (3.3.6) has internal linkage if it is the name of
— a variable, function or function template that is explicitly declared static; or,
On the other hand, static class members would be visible throughout the program.
So, if you would write
struct Constant
{
static ofstream outstream;
}
instead of
namespace Constant
{
static ofstream outstream;
}
it would work.
However, note that the class must have external linkage; e.g. you should not put in in anonymous namespace.
I have two libraries included in my program which both have the same function name, but I need to be able to use both, but I also need C++ to know which one I'm referring to (in certain places I will only be referring to one or the other). The reason why I'm doing this is because I am making my own library and I want to have certain names for my functions, but they are conflicting with functions in someone else's library that I've included, and to make matters worse, some of my functions in my library actually USE the functions in the other persons library which has the same name.
My library is just a .h/.cpp file by the way. Also, when calling MY functions, I don't want any extra luggage such as myNameSpace::myFunc(). I just want to call it myFunc(). However, I don't mind calling the other persons function using a namespace (though I don't want to modify their library in case I break something). (I'm completely new to C++ btw)
HERES MY NEW (TEST - SO FAR) CODE : NOT WORKING W/ ERRORS:
error C2668: 'myFunc' : ambiguous call to overloaded function
main program.cpp
#include "otherslib.h"
#include "mylib.h"
#include <iostream>
using namespace myNamespace;
int main(){
std::cout << myFunc() << std::endl;
return 0;
}
mylib.h
#pragma once
namespace myNamespace{
int myFunc();
}
mylib.cpp
#include "mylib.h"
namespace myNamespace{
int myFunc(){
return 1;
}
}
otherslib.h
#pragma once
int myFunc();
otherslib.cpp
#include "otherslib.h"
int myFunc(){
return 0;
}
You should define your functions in a namespace, and use the namespace when calling them.
namespace myNamespace
{
int myFunc(etc) { ... }
}
int main() {
cout << myNamespace::myFunc();
}
To avoid having to specify your namespace all the time, you could do something like this:
namespace myNamespace
{
int myFunc(etc) { ... }
int main()
{
// Call your own myFunc:
myFunc();
// Call their myFunc:
::myFunc();
}
}