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.
Related
I am trying to link a header file which has a class defined in it and a .cpp file that has the actual functions of that class to my main cpp file but I'm getting this error:
c++ DataMembers.cpp -o DataMembers
Undefined symbols for architecture x86_64:
"Cat::eat()", referenced from:
_main in DataMembers-053507.o
"Cat::meow()", referenced from:
_main in DataMembers-053507.o
"Cat::sleep()", referenced from:
_main in DataMembers-053507.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [DataMembers] Error 1
Please note that all files exists in the same directory!
my main.cpp code is:
#include "Cat.h"
#include <iostream>
using namespace std;
int main()
{
Cat myCat;
myCat.eat();
myCat.meow();
myCat.sleep();
}
my Cat.h code:
#ifndef CAT_H_
#define CAT_H_
class Cat
{
public:
void meow();
void sleep();
void eat();
};
#endif
and Cat.cpp code is:
#include <iostream>
#include "Cat.h"
using namespace std;
void Cat::meow()
{
cout << "Meowwwwww!" << endl;
return;
}
void Cat::sleep()
{
cout << "Zzz ... " << endl;
return;
}
void Cat::eat()
{
cout << "Num Nom Nom .. Yummy" << endl;
return;
}
Interesting thing is when I change the header file in my main.cpp from #inclue "Cat.h"
into #include "Cat.cpp" the program compiles without any issues! I just don't know why?
I have searched for a solution but couldn't find any yet! and I need to be able to use header files I create myself!
Thank you in advance friends!
When compiling make sure that you reference all the cpp source files so it can generate the according object files.
It seems like you forgot to reference cat.cpp. The compiler sees that you have a header file included, assuming the implementation of this class will be in another cpp file. Because you forgot to reference the said file, the linker will complain that some undefined symbols cannot be linked.
Once you included the cat.cpp directly into your main.cpp, you basically copied all its contents to this file, which means there is no need anymore to link the implementation from another translation unit.
In short: you need to tell your compiler about cat.cpp
First Check For Which Architecture You Have Written the Header File For, If it is the Same Architecture as the Main File, Then Try Using Full Path From the Drive, Even then It is Not Working, Add Your Header File Path as an Additional Include Directories.
try this in main.cpp
#include "Cat.cpp"
instead of:
#include "Cat.h"
source:
How to create a C++ project in VS Code and link main, functions and header?
In Cat.cpp you wrote #include "cat.h" but the file name is "Cat.h" so you messed up the capitalization.
recently I've started learning c++. When I tried to write my header file, I got include error. Here is my code:
First is the header file(header.h)
#pragma once
void print(int);
and then is its cpp file(header.cpp)
#include "header.h"
#include <iostream>
using namespace std;
void print(int x){
cout << x << endl;
}
and finally my main cpp program(main.cpp)
#include <iostream>
#include "./header.h"
using namespace std;
int main(){
int x = 123;
print(x);
}
Here is the error, I can't figure out what it's saying orz
cd "/Users/yianchen/Desktop/cpp practice/" && g++ main.cpp -o main &&
"/Users/yianchen/Desktop/cpp practice/"main Undefined symbols for
architecture x86_64: "print(int)", referenced from:
_main in main-90c620.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to
see invocation)
I have searched for some solution, when I use
#include "header.cpp"
It works fine, but I see guys unrecommended using #include some_file.cpp
By the way, I use visual studio code and use code runner. Thanks!
The easiest solution would be to do something like the following
g++ header.cpp main.cpp
This will make sure that the function defined in header.cpp is compiled together with the code that uses it.
Normal usage would be to compile header.cpp, not to include it in another .cpp source. Then the linker will put the pieces together.
I have a simple project in XCode and I am using C++ as the target project.
So I created a main.cpp that has the main function.
Now I wanted to create objects on other files. I keep getting a Linking error.
I am unable to locate the place where this needs to be changed.
main.cpp code
#include <iostream>
#include "Fruits/FruitA.h"
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
createFruitA();
return 0;
}
Now have these files in FruitsA.h and FruitsA.c
#ifndef FruitA_h
#define FruitA_h
#include <stdio.h>
int createFruitA();
#endif /* FruitA_h */
and FruitA.c
#include "FruitA.h"
int createFruitA()
{
printf("I am FruitA\n");
return 0;
}
This is structured in Xcode as follows
I know that we have to add the FruitsA.c for linkage but am unsure as to where it is!
Undefined symbols for architecture x86_64:
"createFruitA()", 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)
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.