Working on a C++ program using chains and having trouble compiling without really knowing why.
The error I get is:
Undefined symbols for architecture x86_64:
"userInputOutput(linearList*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
_main in ccBEoeAc.o
"chain::chain(int)", referenced from:
_main in ccBEoeAc.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
If I use g++ -c main2.cpp, it compiles, but I want to make sure I'm not just covering something up with that option.
Main2.cpp
#include <iostream>
#include "Chain.h"
#include "IOcode.h"
using namespace std;
int main (){
//Call constructor, initial capacity 10
chain *myChain=new chain(10);
//Print initial chain
userInputOutput(myChain, "chain");
}
Chain.cpp
#include "Chain.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include "Myexception.h"
using namespace std;
chain::chain(int initialCapacity)
{
cout<<"This method is working"<<endl;
/* if (initialCapacity <1)
{
//throw illegalParameterValue();
}
firstNode=NULL;
listSize=0;
*/
};
IOcode.cpp
#include <iostream>
#include "Linearlist.h"
using namespace std;
void userInputOutput (linearList* l, string dataStructure){
for (int i = 0; i < 13; i++)
l->insert(i, i+1);
cout<<"The initial "<<dataStructure<<" is: ";
l->traverse();
cout<<"welcome to the assignmet 3!"<<endl;
while(true){
//do stuff
}
}
Any idea why I'm getting these errors? The IOcode file was supplied to me, the chain.cpp I created. Sorry for the poor code, new to C++.
You are just compiling one file.
This should be ok for a quick and dirty try :
g++ main2.cpp Chain.cpp IOcode.cpp
although I would add -Wall -Wextra -pedantic -Werror
Related
I have my main.cc, which is
#include <iostream>
#include "Sally.h"
using namespace std;
int main(){
Sally sallyObject;
sallyObject.printCrap();
}
and header file, Sally.h, which is
#ifndef SALLY_H
#define SALLY_H
class Sally{
public:
Sally();
void printCrap();
protected:
private:
};
#endif //BURRITO_H
and Sally.cc, which is
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally()
{
}
void Sally::printCrap(){
cout << "did someone say steak?" << endl;
}
and those three files are located in same directory.
When I type g++ main.cc, I cannot build the code. It says
Undefined symbols for architecture x86_64:
"Sally::printCrap()", referenced from:
_main in main-16cd07.o
"Sally::Sally()", referenced from:
_main in main-16cd07.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does anybody know why this is happening?
I believe you need to compile all the .cc files together, because sally.cc isn’t being included.
g++ main.cc sally.cc
Should do the trick.
Including the dot-h file makes the definition of your class visible to all (including main.cc) but the actual body of the code is not seen without it being compiled.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am new to stack overflow. I am developing a static c++ library on mac using xcode(9.3). It was compiling and running fine but then i did some code changes and after that its showing some strange compile time errors. like
"/clang:-1: linker command failed with exit code 1 (use -v to see invocation)". Any help will be much appreciated.
Undefined symbols for architecture x86_64:
"LJCPPBL_CORE::LJCPPBL::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
"LJCPPBL_CORE::LJCPPBL::GetShortestPath(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, float*)", referenced from:
_main in main.o
"LJCPPBL_CORE::LJCPPBL::GetJson()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Above text is showing in log.
This is the LJCPPBL header file "LJCPPBL.hpp"
#ifndef LJCPPBL_hpp
#define LJCPPBL_hpp
#include <iostream>
#include "../Models/Models.hpp"
#include <list>
#include <string.h>
using namespace std;
using namespace LJCPPBL_Models;
namespace LJCPPBL_CORE {
class LJCPPBL
{
public: static void Initialize(string clientAPIKEY);
public: static string GetJson();
public: static void SetJson(string strJson);
public: static list<Destination> GetDestinationsList();
public: static list<Point> GetShortestPath(string source, string destination, float* costOfPath);
};
}
#endif /* LJCPPBL_hpp */
This is the Implementation of LJCPPBL header file "LJCPPBL.cpp"
#include <iostream>
#include "LJCPPBL.hpp"
#include "../Models/Models.hpp"
#include "../DAL/DataAccess.cpp"
#include "../Plugins/JsonParser/json.hpp"
#include "SPC.cpp"
#include "GlobalValues.cpp"
#include <list>
using namespace std;
using namespace LJCPPBL_CORE;
using namespace LJCPPBL_DAL;
namespace LJCPPBL_CORE{
void LJCPPBL::Initialize(string clientAPIKEY){
auto globalValues = new GlobalValues();
LJCPPBLGlobalValues = *globalValues;
LJCPPBLGlobalValues.ClientAPIKey = clientAPIKEY;
}
string LJCPPBL::GetJson()
{
LJCPPBLAPIDAL* objDAL = new LJCPPBLAPIDAL();
string mapPointsAndPathJson = objDAL -> GetMapsAndPointsJSON();
string mapDestinationJSON = objDAL -> GetMapDestinationsJSON();
json jsonObj = {{"MapPointsAndPathJSON", mapPointsAndPathJson}, {"MapDestinationJSON", mapDestinationJSON} };
return jsonObj.dump();
}
void LJCPPBL::SetJson(string strJson)
{
}
list<Destination> LJCPPBL::GetDestinationsList()
{
LJCPPBLAPIDAL* objDAL = new LJCPPBLAPIDAL();
return objDAL -> GetDestinations();
}
list<Point> LJCPPBL::GetShortestPath(string source, string destination, float* costOfPath)
{
return SPC::GetShortestPath(source, destination, costOfPath);
}
}
//#endif
Thanks in Advance for your help.
Right-click on the linker error and select "show in log", this will give you real details about the error.
Most likely, it's some unresolved external symbol, so you might not have linked some library or some cpp file to your project.
After edit of question:
There seems to be some library "LJCPPBL" that you have not linked in. At least, you have not linked the core module. You need to set library paths and add library files to your project.
I am trying to include a function called kinematicfactor, written in kinematic_factor.cpp in another program event_rate.cpp.
kinematic_factor.cpp:
#include "kinematic_factor.hpp"
double kinematicfactor (double md) {
double mt = 17.6924; // Mass of Fluorine [GeV/c^2]
double r = 4*mt*md/pow(mt+md, 2);
return r;
}
kinematic_factor.hpp:
#ifndef kinematic_factor_hpp
#define kinematic_factor_hpp
double kinematicfactor (double md);
#endif /* kinematic_factor_hpp */
event_rate:
#include "event_rate.hpp"
#include "kinematic_factor.hpp"
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << kinematic_factor(1.0) << endl;
}
As far as I could check, this was how I was supposed to do it, but I get an error:
Undefined symbols for architecture x86_64:
"kinematicfactor(double)", referenced from:
_main in event_rate-1d17f7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What am I doing wrong?
EDIT: I tried
#include "kinematic_factor.cpp"
And it worked. But I heard that including cpp files (and not header files) is a bad practice for learners... What can I do?
How did you compile this program, I am able to compile same program correctly. I suspect it should be linking issue.
You need to compile source together to compile and link.
g++ kinematic_factor.cpp event_rate.cpp -o event_rate
I am learning C++. I have tried linking separate files in an attempt to have a class in a seperate file.
When I build and run the program I get this error message:
Undefined symbols for architecture x86_64:
"Sally::printCrap()", referenced from:
_main in Main-8bfa94.o
"Sally::Sally()", referenced from:
_main in Main-8bfa94.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]
Here is my code:
Main.cpp
#include <iostream>
#include <string>
#include "Sally.h"
using namespace std;
int main()
{
Sally sallyObject;
Sally *sallyPointer = &sallyObject;
sallyObject.printCrap();
sallyPointer->printCrap();
}
Sally.h
#ifndef SALLY_H
#define SALLY_H
class Sally
{
public:
Sally();
void printCrap();
};
#endif
Sally.cpp
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally()
{
}
void Sally::printCrap(){
cout << "Crap \n";
};
Any help would be greatly appreciated!
Just launch the Terminal application of OS X, then type
cd "/Users/BEASTMACHIENEjr/Desktop/C++ Files" to go to that folder, then type g++ Main.cpp Sally.cpp. This will produce a file called a.out, which you can then launch by typing ./a.out
PS: if you use Sublime Text, I recommend creating a Makefile so you can compile code spanned across multiple source files.
I'm getting this error creating a new object with KarateMatch new_match; when compiling my Driver.cpp file with g++ Driver.cpp KarateMatch.cpp -o output:
Undefined symbols for architecture x86_64:
"KarateMatch::KarateMatch()", referenced from:
_main in cc5G5Ak8.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
KarateMatch new_match;
I can't figure out what I've done wrong, since I am using #include <KarateMatch.h> on both Driver.cpp and KarateMatch.cpp:
Edit:
I've tried the code as exactly shown on my system (OS X 10.8.1), and this isn't compiling with the same error.
KarateMatch.h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class KarateMatch{
public:
KarateMatch();
};
KarateMatch.cpp
#include "KarateMatch.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
KarateMatch::KarateMatch(){
cout << "HI";
}
its running fine on my system g++-4.3..
i think its not the problem with KarateMatch.h and KarateMatch.cpp files.
try compiling only Driver.cpp first without including KarateMatch.h file and without creating its object .