Compiler doesn't see my main function ( Undefined reference to main) - c++

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.

Related

Makefile issues - how to include a function in a different file in your main file

My main source file
myapp.cpp
#include <iostream>
#include <string>
#include "myfunctions.h"
using namespace std;
int main()
{
cout << "Please enter your name \n"; //Prompts user to enter name
helloName();
return EXIT_SUCCESS;
}
the file containing the helloName() function
#include <iostream>
#include <string>
#include "myfunctions.h"
using namespace std;
void helloName()
{
string name; /// string variable defined as 'name'
cin >> name; //User enters name
cout << "Hello " << name << endl; //Outputs "Hello" followed by the name entered
}
my header file for helloName
myfunctions.h
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
void helloName(void);
#endif
Makefile
# makefile for myapp
myapp: myapp.cpp myfunctions.cpp
g++ -ggdb -Wall -std=c++20 -fmodules-ts -o myapp myapp.cpp
myfunctions: myfunctions.cpp myfunctions.h
g++ -ggdb -Wall -std=c++20 -fmodules-ts -o myfunctions myfunctions.cpp
clean:
rm *# *~ myapp
When
make myapp
is entered into terminal, the following error is received:
/usr/bin/ld: /tmp/ccv3S2EK.o: in function main:
myapp.cpp:(.text+0x22): undefined reference to `helloName()'
Can anyone spot my issue? Haven't been coding very long so am sure it is probably basic
There are two problems:
You attempt to build myfunctions.cpp into an executable program named myfunctions.
For myapp you build the executable program using only the myapp.cpp source file.
To solve your problem I recommend you rely on implicit rules to create object files from source files, and link the object files into an executable file.
Then your Makefile only have to look like this:
LD = $(CXX)
myapp: myapp.o myfunctions.o
myapp.o: myapp.cpp myfunctions.h
myfunctions.o: myfunctions.cpp myfunctions.h
The first rule tells make to build the executable file myapp by linking the two object files myapp.o and myfunctions.o. The implicit rules will create the object files from their respective source file.
The second and third rules tells make that the two object files depend on their source file and the header file. So if the header file is modified, then both object files will be rebuilt.
The variable definition tells make that the linker is the same program as the C++ compiler.

Problems with Makefile and compiling a cpp file marks error in a function declared in other file

I have problems with my Makefile.
I used the following structure to generate the .o files of each cpp file, but does not work (using c works without problems, I cant find what is the problem)
%.o : %.cpp %.h
g++ -c -Wall $< -o $#
And the error while compiling is a function is declared in a separated h and cpp file and added to the main file. But when I try to generate de .o file of main.cpp marks error in the function.
The command I used to compile the main.cpp -> g++ -c main.cpp -o main.o
The error that gives me is:
main.cpp: In function ‘int main(int, char)’:
main.cpp:9:9: error: ‘number’ was not declared in this scope9 | number();
This is the compiler that I used for it:
g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Linux 5.15.0-40-generic
Please, anyone could explain me if I'm doing wrong of something is left
/*main.cpp*/
#include <iostream>
#include "numb.h"
using namespace std;
int main(int argc, char* argv[])
{
cout<<"Run"<<endl;
number();
cout<<"end Run"<<endl;
return 0;
}
/*end main.cpp*/
/*numb.cpp*/
#include <iostream>
#include "numb.h"
using namespace std;
int number()
{
cout<<"Function"<<endl;
return 117;
}
/*end numb.cpp*/
/*numb.h*/
#include <iostream>
#define NUMB_H
#ifndef NUMB_H
int number();
#endif
/*end numb.h*/
You got the header guard in the wrong order.
Instead of:
#define NUMB_H
#ifndef NUMB_H
It is supposed to be:
#ifndef NUMB_H
#define NUMB_H
When compiling specify both CPP files, because #include fixes compile errors, but does not fix linker errors
g++ -c main.cpp numb.cpp ...
As a rule, in header files nothing have to be outside the #define guards:
/*numb.h*/
#define NUMB_H
#ifndef NUMB_H
#include <iostream>

undefined reference to constructor c++

I already searched and found a solution for this problem but i find this a little bit strange. Anyway my problem is this:
Personal.h
class Personal
{
public:
Personal();
int money;
~Personal();
}
Personal.cpp
#include "Personal.h"
Personal::Personal()
{
money = 1800;
}
Personal::~Personal(){};
Now i want to compile in main
main.cpp
#include "Personal.h"
#include <iostream>
#include <vector>
int main()
{
std::vector<Personal> test(100);
}
When I write: g++ -Wall main.cpp -o main it gives me :
undefine reference to Personal::Personal()
undefine reference to Personal::~Personal()
The solution:
g++ -Wall Personal.cpp main.cpp -o main
Why do i need compile the Personal.cpp too?
Or the other main version is to include instead of "Personal.h", "Personal.cpp"
main.cpp
#include "Personal.cpp"
#include <iostream>
#include <vector>
Then the normal g++ -Wall main.cpp -o main works
Can someone help me?
Why do i need compile the Personal.cpp too?
Because you use functions that are defined in that file. In particular, you use the functions Personal::Personal and Personal::~Personal.
Can someone help me?
Make sure that all functions (that are odr-used) are defined in exactly one (or in all files, in case of inline functions) of the source files that you compile and link together.

Error: undefined reference to `censorship()' [duplicate]

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)

undefined reference to a class ERROR

I am working in c++ /ubuntu.
I have:
libr.hpp
#ifndef LIBR
#define LIBR
#include <string>
using namespace std;
class name
{
public:
name();
~name();
std::string my_name;
std::string method (std::string s);
};
#endif
and
libr.cpp
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
name::name()
{
}
std::string name::method(std::string s)
{
return ("YOUR NAME IS: "+s);
}
From these two I've created a libr.a.
In test.cpp:
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
int main()
{
name *n = new name();
n->my_name="jack";
cout<<n->method(n->my_name)<<endl;
return 0;
}
I compile with g++ and libr.a. I have an error: "name::name() undefined reference", why?
I would like to mention that I've added in qt creator at qmake the .a. When I compile, I have the error. How can I solve it?
This is a linker error, not a compiler error. It means that you have called but you have not defined the constructor. Your allocation name *n = new name(); calls the constructor.
Since you defined the constructor in your libr.cpp, what this means is that this compilation unit is not making its way into your executable. You mentioned that you are compiling with libr.a. When you compile your libr.cpp the result is a .o file, not a .a file.
You are not linking libr.o into your executable.
What are the steps you're using to compile your "project"?
I performed the following steps and managed to build it with warnings/errors.
g++ -Wall -c libr.cpp
ar -cvq libr.a libr.o
g++ -Wall -o libr main.cpp libr.a
One last thing, if I change the order off the last command, like
g++ -Wall -o libr libr.a main.cpp
I get the following error:
Undefined first referenced
symbol in file
name::name() /tmp/cc4Ro1ZM.o
name::method(std::basic_string<char, std::char_traits<char>, std::allocator<char
> >)/tmp/cc4Ro1ZM.o
ld: fatal: Symbol referencing errors. No output written to libr
collect2: ld returned 1 exit status
in fact , you needn't define the destructor yourself because the default destructor will be used when the class calling is over.
and in the VS2008,it's all right!