So I'm just writing a simple c++ program and I am having some issues running the program. I could build my make file but when I try to run the testfile it gives me an "collect2.exe: error: ld returned 1 exit status" error, "undefined reference to Account::Account(int, double, double)" and basically all of my methods. I've attached a picture of my .h file, and .cpp below.
.h File
.cpp file
And the test file is like this :
#include <iostream>
#include <string>
#include <iomanip>
#include "Account.h"
using namespace std;
int main(){
Account account1(1122, 20000, 4.5);
account1.withdraw(2500);
account1.deposit(3000);
cout<<"Account Information"<<endl
<<"ID: " << account1.getID()<<endl
<<"Balance: " << account1.getBalance()<<endl
<<"Monthly Interest Rate: " << account1.getMonthlyInterestRate()<< endl;
return 0;
}
Make file:
TestAccount: TestAccount.o Account.o
g++ TestAccount.o Account.o -o TestAccount
TestAccount.o: TestAccount.cpp Account.h
g++ -c TestAccount.cpp
Account.o: Account.cpp Account.h
g++ -c Account.cpp
Related
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
I have 2 cpp and 3 header files in my project. When I compile them in VS it works smoothly and I get no error message. But when I try to compile it on SSH network by this line:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
It says:
In function `_start':
(.text+0x20): undefined reference to `main'
Do not say "do not forget to write main function" because it is already there and my project works on VS. What to do then?
Here is related part from my codes. Program.cpp until main:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include "lineoperations.h"
using namespace std;
line bankline;
bool operate(char);
void search(char[]);
void add(char[]);
void removee(char[]);
void transaction();
void printline();
int main(){
bankline.create();
bool end = false;
while (!end) {
end = bankline.decideFunction();
}
bankline.close();
return EXIT_SUCCESS;
}
It goes on but it is not necessary to paste them I guess. If you need to see other cpp file or header files I'll paste them as well.
The command:
g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
tells g++ to compile and link the files:
lineoperations.cpp customer.h transaction.h lineoperations.h
and output an executable program called program.cpp.
This fails with the linkage error you have observed because
main is defined in program.cpp, which you are not compiling or linking.
Try this instead:
g++ -o program program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
or if you are on Windows:
g++ -o program.exe program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
And BTW, there is no need to list the header files on the commandline. They are included by
the source files, I presume.
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
Trying to compile my program via g++ -o prog1 main.cpp -std=c++0x
I get the error:
/tmp/cc1pZ8OM.o: In function `main':
main.cpp:(.text+0x148): undefined reference to `Hash::insert(int, char)'
collect2: error: ld returned 1 exit status
main.cpp
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <functional>
#include "Hash.h"
using namespace std;
int main(int argc, char *argv[]) {
//preset prime number
int prime = 101;
hash<char> h1;
int key;
Hash HashTable;
// check for Request & string parameters
if(argc != 3) {
cout << "Run program with 2 parameters. [Lower Case]" << endl;
cout << "[1] insert, find, or delete" << endl;
cout << "[2] string" << endl;
}
if(strcmp(argv[1], "insert") == 0) {
//Get Hash for argv[2] aka value
key = h1(*argv[2]);
//check 1
cout << "Hash: " << key << endl;
key = key % prime;
//check 2
cout << "Mod 101 Hash: " << key << endl;
HashTable.insert(key, *argv[2]); //PROBLEM here
}
return 0;
}
Hash.h file:
#include <iostream>
#include <cstring>
#include "LinkedList.h"
using namespace std;
class Hash {
//100 slot array for hash function
LinkedList *hashFN[100];
public:
void insert(int key, char value);
//void deleteItem(int key);
//char* find(int key);
};
Any ideas? Using this to build a hash table with set size.
Edit: Hash.cpp file
#include <iostream>
#include <cstring>
#include "Hash.h"
using namespace std;
void Hash::insert(int key, char value){
*hashFN[key]->addFront(value);
cout << "Success!" << endl;
}
Trying to compile via terminal now with:
g++ -c Hash.cpp -o Hash.o
g++ -o prog1 main.cpp Hash.o -std=c++0x
It goes into an infinite loop somehow.
Your header file Hash.h declares "what class hash should look like", but not its implementation, which is (presumably) in some other source file we'll call Hash.cpp. By including the header in your main file, the compiler is informed of the description of class Hash when compiling the file, but not how class Hash actually works. When the linker tries to create the entire program, it then complains that the implementation (toHash::insert(int, char)) cannot be found.
The solution is to link all the files together when creating the actual program binary. When using the g++ frontend, you can do this by specifying all the source files together on the command line. For example:
g++ -o main Hash.cpp main.cpp
will create the main program called "main".
This error tells you everything:
undefined reference toHash::insert(int, char)
You're not linking with the implementations of functions defined in Hash.h. Don't you have a Hash.cpp to also compile and link?
Your error shows you are not compiling file with the definition of the insert function. Update your command to include the file which contains the definition of that function and it should work.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have main.cpp:
#include "censorship_dec.h"
using namespace std;
int main () {
censorship();
return 0;
}
this is my censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void censorship();
this is my censorship_mng.cpp:
#include "censorship_dec.h"
using namespace std;
void censorship()
{
cout << "bla bla bla" << endl;
}
I tried to run these files in SSH (Linux), so I wrote: make main, but I got:
g++ main.cpp -o main
/tmp/ccULJJMO.o: In function `main':
main.cpp:(.text+0x71): undefined reference to `censorship()'
collect2: ld returned 1 exit status
make: *** [main] Error 1
please help!
You have to specify the file where censorship is defined.
g++ main.cpp censorship_mng.cpp -o main
You must add censorship_mng.cpp in your compilation command:
g++ main.cpp censorship_mng.cpp -o main
Another solution (if you really don't want change your compile command) is making void censorship(); to a inline function and move it from .cpp to .h.
censorship_dec.h:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
inline void censorship()
{
// your code
}
And remove void censorship() from censorship_mng.cpp file.
once your project starts using several source-files to be compiled into a single binary, manual compilations become tedious.
this is usually the time when you start using a build-system, such as a Makefile
a very simple Makefile that uses default build-rules could look like
default: main
# these flags are here only for illustration purposes
CPPFLAGS=-I/usr/include
CFLAGS=-g -O3
CXXFLAGS=-g -O3
LDFLAGS=-lm
# objects (.o files) will be compiled automatically from matching .c and .cpp files
OBJECTS=bar.o bla.o foo.o main.o
# application "main" build-depends on all the objects (and linksthem together)
main: $(OBJECTS)