C++ Undefined Reference When Compiling .h and .cpp in Subfolders - c++

Long story short I want to put my .h and .cpp files in subfolders (include and src respectively) and reference them in my main.cpp file but I am receiving an error of:
main.cpp:(.text+0x47): undefined reference to `Kmer::Kmer()'.
when compiling using:
g++ -I /path/to/MyFolder/include main.cpp.
My files are structured like below:
MyFolder
main.cpp
include
Kmer.h
src
Kmer.cpp
//main.cpp
#include <iostream>
#include "Kmer.h"
using namespace std;
int main() {
Kmer k;
return 0;
};
//Kmer.h
#pragma once
class Kmer{
public:
Kmer();
protected:
private:
};
//Kmer.cpp
#include "Kmer.h"
#include <iostream>
using namespace std;
Kmer::Kmer(){
// code here
cout << "Kmer created" << endl;
}
I appreciate the help!

You are not compiling Khmer.cpp. You need to add it to your g++ compile line
g++ -o <YOUR APPLICATION NAME> -I /path/to/MyFolder/include main.cpp src/Khmer.cpp

Related

Using same namespace in C++ for two classes declared and defined in separate files

I am trying to declare two classes C1 and C2 in files nstest1.h and nstest2.h which are defined in files nstest1.cpp and nstest2.cpp respectively. Both the classes are defined under same namespace.
Following are the files :
//nstest1.h
namespace Mine{
class C1{
public:
void callme();
};
}
//nstest2.h
namespace Mine {
class C2 {
public:
void callme();
};
}
//nstest1.cpp
#include<iostream>
#include "nstest1.h"
using namespace std;
using namespace Mine;
void Mine::C1::callme(){
std::cout << "Please call me " << std::endl;
}
//nstest2.cpp
#include<iostream>
#include "nstest2.h"
using namespace std;
using namespace Mine;
void Mine::C2::callme(){
std::cout << "Please call me too" << std::endl ;
}
Following file tries to use this classes using namespace Mine.
//nstest.cpp
#include<iostream>
#include "nstest1.h"
#include "nstest2.h"
using namespace std;
using namespace Mine;
int main(){
Mine::C1 c1;
Mine::C2 c2;
c1.callme();
c2.callme();
return 0;
}
When I compile using command "g++ nstest.cpp", I get following error :
/tmp/cc2y4zc6.o: In function `main':
nstest.cpp:(.text+0x10): undefined reference to `Mine::C1::callme()'
nstest.cpp:(.text+0x1c): undefined reference to `Mine::C2::callme()'
collect2: error: ld returned 1 exit status
If the definitions are moved to the declaration files (nstest1.h and nstest2.h), it works fine. Not sure whats happening here. Am I missing something ?
Thanks in advance :) .
You need to include the other .cpp files when building the program.
Option 1: Compile all the files and build the executable in one command
g++ nstest.cpp nstest1.cpp nstest2.cpp -o nstest
Option 2: Compile each file separately and then build the executable after that
g++ -c nstext1.cpp
g++ -c nstest2.cpp
g++ -c nstest.cpp
g++ nstest.o nstest1.o nstext2.o -o nstest
Your problem happens at link time. Your headers are fine. But you should compile the other cpp files aswell.

Error when trying to separating class into .h, .cpp

This is a minimal program that I made to understand this problem better.
ADT.h
#ifndef ADT_H
#define ADT_H
class ADT {
public:
void print();
};
#endif
ADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
void ADT::print()
{
cout << "This program works." << endl;
}
testADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
int main(void)
{
ADT sa;
sa.print();
return 0;
}
I compiled it with the vim/minGW compiler my school provided me like so:
g++ testADT.cpp
Which produced the following error:
C:\Users\King\AppData\Local\Tempcc6eoWAP.o:testADT.cpp(.text+0x15 reference to 'ADT::print()'
collect2.exe error: ld returned 1 exit status
Can you explain this error message and indicate the error in my code?
You didn't post the error, but I see that you're missing the semicolon after void print()in the header.
EDIT: That's a linker error. Each source file should be compiled into an object file; then the object files linked:
g++ -c -oADT.o ADT.cpp
g++ -c -otestADT.o testADT.cpp
g++ -oADT ADT.o testADT.o
You can also do it in one line as in michaeltang's answer, but then you can't recompile the sources individually (the 2 step method scales better).
You should also compile ADT.cpp
g++ -o testadt testADT.cpp ADT.cpp

undefined reference to class::function when I try to compile

I have three files: Main.cpp, Security.h, and Security.cpp.
I have declared my class Security (including a function) in my header file.
I have defined the function in Security.cpp.
My header file has been included in both Main.cpp and Security.cpp.
In Main.cpp, I'm creating an object, and attempting to run the member function and keep getting a compile error.
Main.cpp
#include<iostream>
#include "Security.h"
using namespace std;
int main()
{
Security S1;
S1.Driver();
}
Security.h
class Security
{private:
public:
void Driver();
};
Security.cpp
#include<iostream>
#include<fstream>
#include "Security.h"
using namespace std;
void Securtiy::Driver()
{
cout << "Enter a number: ";
int answer;
cin >> answer;
cout << answer;
}
You should compile both files, because the definition of Security::Driver is in Security.cpp.
The easiest way would be to invoke a single command:
g++ Main.cpp Security.cpp
However, if you want to compile the files separately, you must compile them into an intermediate ('object') format using -c flag:
g++ -c Main.cpp
g++ -c Security.cpp
This will give you two object files. Now link them:
g++ Main.o Security.o
Securtiy is a misspelling in your Security.cpp file.

How to use header files?

I wanted to learn using header files. and I got an error. here is my code:
printmyname.h:
void printMyName();
printmyname.cpp:
#include "printmyname.h"
void printMyName() {
cout << "omer";
}
try.cpp (main file):
#include <iostream>
#include "printmyname.h"
using namespace std;
int main() {
printMyName();
return 0;
}
Here is the error:
undefined reference to `printMyName()`
What's is the problem?
Undefine reference has nothing to do with your header file in this case. It means the linker cannot find the implementation of printMyName which is in printmyname.cpp. If you are using g++, you should try:
g++ try.cpp printmyname.cpp -o yourBinaryName
If you are using a makefile, you should add dependency(printmyname.cpp) correctly for try.cpp.
Edit:
As #zmo suggest in his comment:
you can also do it through a two times compilation (more suitable with Makefiles):
g++ -c printmyname.cpp
g++ try.cpp printmyname.o -o yourBinaryName
If you are using Windows, you need to add the printmyname.cpp to your project too.
Consider adding an include guard to your header
#ifndef PRINTMYNAME_INCLUDED
#define PRINTMYNAME_INCLUDED
void printMyName();
#endif
You will also need to move the #include <iostream> and using namespace std; from the try.cpp to the printmyname.cpp file.
You need to add code/definition in printMyName.cpp inside printMyName.h only.
void printMyName();
{
cout << "omer";
}

g++ is unable to find my header file

I'm trying to compile the file q1.cpp but I keep getting the compilation error:
q1.cpp:2:28: fatal error: SavingsAccount.h: No such file or directory
compilation terminated.
The header file and the implementation of the header file are both in the exact same directory as q1.cpp.
The files are as follows:
q1.cpp:
#include <iostream>
#include <SavingsAccount.h>
using namespace std;
int main() {
SavingsAccount s1(2000.00);
SavingsAccount s2(3000.00);
}
SavingsAccount.cpp:
#include <iostream>
#include <SavingsAccount.h>
using namespace std;
//constrauctor
SavingsAccount::SavingsAccount(double initialBalance) {
savingsBalance = initialBalance;
}
SavingsAccount::calculateMonthlyInterest() {
return savingsBalance*annualInterestRate/12
}
SavingsAccount::modifyInterestRate(double new_rate) {
annualInterestRate = new_rate;
}
SavingsAccount.h:
class SavingsAccount {
public:
double annualInterestRate;
SavingsAccount(double);
double calculateMonthlyInterest();
double modifyInterestRate(double);
private:
double savingsBalance;
};
I'd like to reiterate that all files are in the SAME directory. I'm trying to compile by using this line at a windows command prompt:
C:\MinGW\bin\g++ q1.cpp -o q1
Any input into this problem would be appreciated.
#include <SavingsAccount.h>
should be
#include "SavingsAccount.h"
since SavingsAccount.h is the header file you defined, you should not ask the compiler to search for system headers by using <> around it.
Meanwhile, when you compile it, you should compile both cpp files: SavingsAccount.cpp and q1.cpp.
g++ SavingsAccount.cpp q1.cpp -o q1
BTW: you missed a ; here:
SavingsAccount::calculateMonthlyInterest() {
return savingsBalance*annualInterestRate/12;
//^^; cannot miss it
}