Is there something missing from this extremely basic but uncooperative code? - c++

I recently started learning C++, and I'm currently trying to test out the header file functionality. I wrote three simple files:
headertest.h:
#pragma once
class Cube
{
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
headertest.cpp:
#include "headertest.h"
double Cube::getVolume()
{
return length_ * length_ * length_;
}
double Cube::getSurfaceArea()
{
return 6 * length_ * length_;
}
void Cube::setLength(double length)
{
length_ = length;
}
and main.cpp:
#include "headertest.h"
#include <iostream>
int main()
{
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << volume;
}
However, when I try to build the files, I get the following error:
> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/jonathan/Desktop/myc++/cubetest/headertest.cpp -o /Users/jonathan/Desktop/myc++/cubetest/headertest <
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Is there something wrong about how I wrote the main.cpp file, or am I leaving out something important? I've spent all day trying to figure out this issue, and I have no idea how to get unstuck. Thank you!!

Related

Clang unable to link files in VS Code

I recently started learning C++, and I'm currently trying to test out the header file functionality. I wrote three simple files:
headertest.h:
#pragma once
class Cube
{
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
headertest.cpp:
#include "headertest.h"
double Cube::getVolume()
{
return length_ * length_ * length_;
}
double Cube::getSurfaceArea()
{
return 6 * length_ * length_;
}
void Cube::setLength(double length)
{
length_ = length;
}
and main.cpp:
#include "headertest.h"
#include <iostream>
int main()
{
Cube c;
c.setLength(3.48);
double volume = c.getVolume();
std::cout << volume;
}
However, when I try to build the files with Clang, I get the following error:
> Executing task: /usr/bin/clang++ -std=c++17 -stdlib=libc++ -g /Users/jonathan/Desktop/myc++/cubetest/headertest.cpp -o /Users/jonathan/Desktop/myc++/cubetest/headertest <
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it.
Does anyone have any idea of why this is happening? I've tried running g++ main.cpp headertest.cpp -o main before building, but the error isn't getting solved. Thank you!

Xcode 11 project on MacOS not to use sin and cos in one function: undefined symbol "___sincosf_stret"

Building my libraries on Mac suddenly started to fail with Xcode Version 11.3 (11C29).
I was able to isolate one problem:
It seems that when you use sin and cos in the same function, the optimizer will use __sincosf_stret to calculate both at once.
So what did I do:
Create a new Xcode project, cmd line tool, objective-c
Change the main.m to main.mm to allow C++
Change main as:
#import <Foundation/Foundation.h>
#include <cmath>
void coordinateCalculator(float angle, float radius, float& x, float& y) {
float mySin = sin(angle);
float myCos = cos(angle);
x = radius * myCos;
y = radius * mySin;
}
int main(int argc, const char * argv[]) {
#autoreleasepool {
float myAngle = 0.0;
float theX, theY;
float radius = 100.0;
while (myAngle < 360.0) {
coordinateCalculator(myAngle, radius, theX, theY);
NSLog(#"My coordinates: %f, %f",theX,theY);
myAngle += 1.0;
}
}
return 0;
}
Building in Debug works fine, building for profile (Release version with optimization) will fail giving the following error message:
Undefined symbols for architecture x86_64:
"___sincosf_stret", referenced from:
coordinateCalculator(float, float, float&, float&) in main.o
_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'm building with:
C++: C++14, libC++
Base SDK: MacOS
Deployment target 10.14
When I change the deployment target to 10.7, the sincosf_stret is found, but I get 2 ARC errors:
Undefined symbols for architecture x86_64:
"_objc_loadClassref", referenced from:
__ARCLite__load() in libarclite_macosx.a(arclite.o)
"_objc_readClassPair", referenced from:
__ARCLite__load() in libarclite_macosx.a(arclite.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Playing with the deployment SDKs give me:
10.7: 2 ARC link errors
10.10: 2 ARC link errors AND _sincosf_ret error
10.12 or higher: _sincosf_ret error
I can't belief that it is not possible to use sin/cos in one function.
Re-installing XCode solved my issue. Another developer had tried it on his system and it seemed to work. That made me decide to delete XCode and install it again. Problem is gone now. No problem with XCode itself.

Compiling libzip on Mac: Undefined symbols for architecture x86_64

I've been learning C++ and have decided to try to create a simple file reader using libzip on archive files (e.g. Word).
I’ve recently installed libzip on my Macbook using brew but I seem to keep on getting the following issue whenever I try to compile a program that uses libzip:
Undefined symbols for architecture x86_64:
"_zip_fopen", referenced from:
_main in main-918bfa.o
"_zip_open", referenced from:
_main in main-918bfa.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: *** [a.exe] Error 1
The command I use to compile:
g++ -g main.cpp -std=c++11 -I/usr/local/Cellar/libzip/0.11.2/include -I/usr/local/Cellar/libzip/0.11.2/lib/libzip/include -L/usr/local/Cellar/libzip/0.11.2/lib -o ../a.exe
main.cpp:
#include <iostream>
#include <fstream>
#include <zip.h>
#include <zlib.h>
using namespace std;
int numArgs = 2;
int main(int argc, char** argv){
// Parse command line arguments
if(argc != numArgs){
std::cout << "Incorrect number of arguments provided.\n";
std::cout << "Command line syntax: fileReader.exe inputFile" << endl;
exit(0);
}
// Try out libzip functionality
std::string inputDocument(argv[1]);
int err = 0;
zip* z = zip_open(inputDocument.c_str(), 0, &err);
if(z == NULL) {
printf("Could not read docx file. Error code: %d", err);
exit(-1);
}
zip_file* contentTypes = zip_fopen(z, "[Content_Types].xml", ZIP_FL_UNCHANGED);
exit(0);
}
Doesn't look like your including the libzip library in the compilation command. Try adding -lzip to your g++ command

c++ Compile error - Undefined symbols for architecture x86_64 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined symbols for architecture i386:
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include "CorpusExp.h"
int main(int argc, char* argv[]){
ifstream infile("../DATA.txt");
string train_dir; // The training data directory
infile>>train_dir;
train_dir=train_dir.substr(6,train_dir.length());
if (argc>1){
if (strcmp(argv[1],"-s")){ // enter into CorpusExploration mode
CorpusExp ce(train_dir,"all"); //<<=======LINE X!!!!!!!!
//ce.calculate();
if (argc>=3 && strcmp(argv[2],"-u")){ // check user stats
cout<<"shit";
}
else if (argc>=3 && strcmp(argv[2],"-m")){ // check movie stats
}else{ // check the all (default) stats
}
}else if(strcmp(argv[1],"-m")) {// enter into Recommendation mode
}
}
return 0;
}
If I include the LINE X, the main.cpp won't compile, the error message is:
Undefined symbols for architecture x86_64:
"CorpusExp::CorpusExp(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [recommend] Error 1
And here is my CorpusExp.cpp and CorpusExp.h:
/* CorpusExporater header file
Used for statistical purpose
*/
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h> // the Linux library to read the directories
class CorpusExp{
public:
CorpusExp(const string&,const string&); // (dir_name, mode)
void calculate();
void display(ostream &out, const int&); // print out the id's stat information
private:
string _dir_name;
int _movie_count;
int _user_count;
int _movie_rated_count[5]; // times of movie rated as 1,2,3,4,5
float _movie_avg; // total moview average rates
string _mode; // the mode of current task
int _id; // the id of current task
};
/*CorpusExporater cpp file
Used for statistical purpose
*/
#include "CorpusExp.h"
CorpusExp::CorpusExp(const string &dir_name, const string &mode):
_dir_name(dir_name),
_mode(mode),
_movie_count(0),
_user_count(0)
{
}
void CorpusExp::calculate(){
DIR *dpdf;
struct dirent *epdf;
dpdf = opendir(strdup(_dir_name.c_str()));
if (dpdf!=NULL){
while(epdf = readdir(dpdf)){
cout<<epdf->d_name<<endl;
}
}
}
void CorpusExp::display(ostream &out, const int &id){
}
I am barely new to c++, so I get very confused, don't know which part caused this problem. If I remove the LINE X, everything will just be fine, but if I declare the CorpusExp class in the main function, then it did not compile......
here is the compile file:
# Makefile for Movie Recommendation--- HW5 of Search Engine 11-641
# Yitong Zhou
# Nov 19, 2012
#source code directory:
src=src
#obj code directory:
obj=obj
recommend: $(obj)/main.o $(obj)/CorpusExp.o
g++ -o $# $<
$(obj)/main.o: src/main.cpp src/CorpusExp.cpp
g++ -c $< -o $#
$(obj)/CorpusExp.o: $(src)/CorpusExp.cpp $(src)/CorpusExp.h
g++ -c $< -o $#
clean:
rm -rf obj/*.o recommend
Try a simple command line compile without the makefile to make sure the makefile itself isn't the problem.
cd src; g++ main.cpp CorpusExp.cpp -o recommend

C++ libPNG - simple initialisation error

I am getting the following error when trying to compile....
Undefined symbols for architecture x86_64:
"_png_sig_cmp", referenced from:
RenderUtils::isValidPng(std::istream&) in RenderUtils.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
my code is as follows:
//called from here
ifstream s;
s.open("/Users/tmg06qyu/Desktop/texture_512.png", ios::binary);
if(!RenderUtils::isValidPng(s)){
throw 20;
}
//header
class RenderUtils{
public:
static bool isValidPng(std::istream &source);
};
//implementation
#include <iostream>
#include "RenderUtils.h"
#include "png.h"
#define PNGSIGSIZE 8
using namespace std;
bool RenderUtils::isValidPng(std::istream &source){
//Allocate a buffer of 8 bytes, where we can put the file signature.
png_byte pngsig[PNGSIGSIZE];
int is_png = 0;
//Read the 8 bytes from the stream into the sig buffer.
source.read((char*)pngsig, PNGSIGSIZE);
//Check if the read worked...
if (!source.good()) return false;
//Let LibPNG check the sig. If this function returns 0, everything is OK.
is_png = png_sig_cmp(pngsig, 0, PNGSIGSIZE);
return (is_png == 0);
}
My guess is that you built a 32-bit version of libpng, but now you are trying to link 64-bit code with it. Try file * or otool -L * to check (from memory)
Sorry everyone....stupid me. I needed to link against zlib.....note to self.....always read the readme....(well not always!)