This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
This is the input to g++ and the resulting error messages.
$ g++ main.cpp -o keyLogger
/tmp/ccvwRl3A.o:main.cpp:(.text+0x93): undefined reference to `SaveFeatures::SaveFeatures(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0x93): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SaveFeatures::SaveFeatures(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0xbf): undefined reference to `SaveFeatures::save(std::string)'
/tmp/ccvwRl3A.o:main.cpp:(.text+0xbf): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SaveFeatures::save(std::string)'
collect2: error: ld returned 1 exit status
I've checked and rechecked my syntax for the .h and .cpp for SaveFeatures class but havent been able to find an error. Any help would be welcome.
Main.cpp
#include <string>
#include "SaveFeatures.h"
using namespace std;
int main(){
string fileName="saveTest.text";
string saveContent="this is a test";
SaveFeatures saveFeatures(fileName);
saveFeatures.save(saveContent);
}
SaveFeature.cpp
#include "SaveFeatures.h"
#include <string>
using namespace std;
SaveFeatures::SaveFeatures(string fileName){
setFileName(fileName);
}
void SaveFeatures::setFileName(string fileName){
if(fileName!=NULL){
this.fileName=fileName;
}
}
bool SaveFeatures::save(string content){
if(fileName==NULL)return false;
if (content==NULL)return false;
FILE *file;
file=fopen(fileName,"a");
if(file!=NULL){
fputs(content,file);
}
return true;
}
string SaveFeatures::getFileName(){
return fileName;
}
SaveFeatures.h
#ifndef SAVEFEATURES_H
#define SAVEFEATURES_H
#include <string>
using namespace std;
class SaveFeatures{
public:
SaveFeatures(string fileName);
void setFileName(string fileName);
string getFileName();
bool save(string content);
private:
string fileName;
//need to make a method to determine if the fileName has file extension
};
#endif
You will need to specify all the needed source files for your executable.
g++ main.cpp SaveFeature.cpp -o keyLogger
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I am writing a simple program to compile from the command prompt. There is an app1 program that uses a class located in a separate file. The class sample1 has a header file sample1.h where all the function declarations exist. The sample1.cpp file contains the function definitions.
app1.cpp:
#include <iostream>
#include "sample1.h"
using namespace std;
int main()
{
sample1 t;
cout<<"The program is ending"<<endl;
return 0;
}
sample1.h:
#ifndef SAMPLE1_H
#define SAMPLE1_H
class sample1
{
public:
sample1();
};
#endif
sample1.cpp:
#include<sample1.h>
#include <iostream>
using namespace std;
sample1::sample1()
{
cout<<"This error sucks"<<endl;
}
I am compiling this program using Windows Subsystem for Linux. However, when I try to compile with g++ 'app1.cpp' -o app1, I get this error:
I get that the error is telling me that the program cannot find the constructor sample::sample() but why is that? I included the header file in the program. These 3 files all exist in the same folder.
You have to compile all your .cpp files (sample1.h is included by preprocessor from information in app1.cpp only):
g++ app1.cpp sample1.cpp -o app1
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I'm learning C++ following this link: https://www.youtube.com/watch?v=mUQZ1qmKlLY. Now I'm confused about member initialization. So, I have three files, main.cpp, Sally.h, Sally.cpp. as the following.
main.cpp
#include <iostream>
#include "Sally.h"
using namespace std;
int main(){
Sally so(3,87);
so.print();
}
Sally.h
#ifndef SALLY_H
#define SALLY_H
class Sally
{
public:
Sally(int a, int b);
void print();
private:
int regVar;
const int constVar;
};
#endif
Sally.cpp
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally(int a, int b)
:regVar(a), constVar(b)
{
}
void Sally::print()
{ cout << "regulat var is: " << regVar << "const var is:" << constVar << endl;
}
When I run the main.cpp file, it does not give me any prints out. Instead, it gives me the following message.
$ g++ main.cpp
/tmp/ccyvg9rV.o: In function `main':
main.cpp:(.text+0x29): undefined reference to `Sally::Sally(int, int)'
main.cpp:(.text+0x35): undefined reference to `Sally::print()'
collect2: error: ld returned 1 exit status
Moreover, why do not I see anything similar to this member initialization in other languages, like Java, Julia or Python?
When I run the main.cpp file
g++ main.cpp
That does not run the file, it compiles and links it.
Since main.cpp is not a full program (you also need Sally.cpp), the linker tells you you have undefined references.
Instead, do:
g++ Sally.cpp main.cpp
And you should get a binary/executable in your current folder that you can run.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
well I want to understand linking with headers and other .cpp (functions for example) so my quastion is why I get "undefined reference to 'afis(). There are sample example and I want to clarify this. Also sorry for my bad english :D.
There is main:
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
afis();
return 0;
}
There is an function named function.cpp:
#include <iostream>
using namespace std;
void afis(){
cout <<"yehe";
}
And there is the header :
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void afis();
#endif // FUNCTIONS_H_INCLUDED
While the C++ compiler automatically "pulls in" referenced header files, it can't do that for the actual .cpp code files.
Instead of calling
CXX/clang++/g++ main.cpp -o hello
you need to manually include all relevant code files:
CXX/clang++/g++ main.cpp functions.cpp -o hello
I m trying to compile this code and linking fails with the following error:
this is how i m compiling it;
g++ logtester.cc -I/home/foo/include -L/home/foo/lib -llog4cxx
/tmp/ccADKreY.o(.text+0x120): In function `main': undefined reference to `FrameworkLogger::getInstance()'
collect2: ld returned 1 exit status
Why? how can i fix it?
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::helpers;
class FrameworkLogger
{
private:
FrameworkLogger();
LoggerPtr logger;
public:
static LoggerPtr getInstance();
};
(Another file:)
#include "FrameworkLogger.h"
#include <iostream>
LoggerPtr FrameworkLogger::getInstance()
{
std::cout<<"test";
}
(Yet another file:)
#include "FrameworkLogger.h"
#include <iostream>
using namespace std;
int main(){
// LoggerPtr logger =
FrameworkLogger::getInstance();
std::cout<<"test";
}
This sounds like a linker error. Ensure that you are properly linking all of your object files
You need to list all compilation units (.cc files) in the compiler invocation:
g++ logtester.cc the-file-you-have-not-named.cc -I/home/foo/include -L/home/foo/lib -llog4cxx
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!