Error on compiling file in sublime text on macOS - c++

I am new to programming and have just installed this text editor on a friend's suggestion.
I don't know what went wrong but I get this error on building a simple C++ code.
#include <iostream>
using namespace std;
int main(){
int a;
cin>>a;
for(int i=0;i<a;i++)
cout<<"HEllo World!";
return 0;
}
The error is as follows:-
ld: can't write output file to '/Users/vihangawagholkar/Desktop/dsa/test' because that path is a directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 234ms with exit code 1]
[shell_cmd: g++ "/Users/vihangawagholkar/Desktop/dsa/test.cpp" -o "/Users/vihangawagholkar/Desktop/dsa/test" && "/Users/vihangawagholkar/Desktop/dsa/test"]
[dir: /Users/vihangawagholkar/Desktop/dsa]
[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin]

Related

g++ linker can not find library

I want to use this library in my c++/test.cpp file.
#include "omp/HandEvaluator.h"
#include <iostream>
using namespace omp;
int main()
{
HandEvaluator eval;
Hand h = Hand::empty(); // Final hand must include empty() exactly once!
h += Hand(51) + Hand(48) + Hand(0) + Hand(1) + Hand(2); // AdAs2s2h2c
std::cout << eval.evaluate(h) << std::endl; // 28684 = 7 * 4096 + 12
}
I downloaded the source code from github and placed into the OMPEval folder. After make the ompeval.a library appeared.
Here is the folder structure:
Now I try to build it:
projects/c++$ g++ -Wall -g -L /home/a/projects/c++/OMPEval/lib/ -l ompeval -I /home/a/projects/c++/OMPEval/ test.cpp -v
but the linker has error:
/usr/bin/ld: cannot find -lompeval
collect2: error: ld returned 1 exit status
Here is the whole build log:

freopen() not able to write to a file?

On MacOSX, I'm trying to read input from inp.txt and write output to outp.txt on Sublime.
I'm redirecting stdout to outp.txt
#include<iostream>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("inp.txt", "r", stdin);
// for writing output to output.txt
freopen("outp.txt", "w", stdout);
#endif
int n;
cin>>n;
cout<<n+1;
}
Error is
ld: can't open output file for writing: /Users/sarath/Documents/code, errno=21 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 2.9s with exit code 1]
[shell_cmd: g++ "/Users/sarath/Documents/code.cpp" -o "/Users/sarath/Documents/code" && "/Users/sarath/Documents/code"]
[dir: /Users/sarath/Documents]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
Why it is failing and please help ?
ThankYou.

Build Fail- Error Message Translatopn

I recently made the switch from a PC to a Mac, Visual Studio to Netbeans, and Java to C++. I tried to include a boost library into my program, and when I build my code, I receive a build error. Can someone please walk me through what this build error is saying? I followed this post to add the libraries. I also followed this Boost getting start tutorial, and the Boost folder is in the "Netbeans Projects" folder, this is the directory "/Users/Nate/NetBeansProjects/boost_1_60_0/boost". Should the boost files have been placed somewhere else?
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/stockapp
mkdir -p dist/Debug/GNU-MacOSX
g++ -o dist/Debug/GNU-MacOSX/stockapp build/Debug/GNU-MacOSX/main.o -L../boost_1_60_0/boost -l boost
ld: library not found for -lboost
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-MacOSX/stockapp] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 213ms)
I am trying to build an program that will download website HTML and parse the HTML to retrieve stock prices from fiance.yahoo.com, here is the unfinished code:
using namespace std;
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/asio.hpp> //code worked before adding this include
static string Index; //initialize string hosting Index Name
static vector<string> Symbol;
int ReadIndexFile()
{
string FileRead;
string FileName;
//string Temp;
int count = 0;
FileName = Index + "Symbols.csv";
cout << FileName << "\n";
ifstream source(FileName.c_str());//establishes source file
while (!source.eof()) //reads source until end of file
{
while (getline(source, FileRead, ','))//retrieves source data to ',' and stores in temp
{
Symbol.push_back(FileRead); //writes to array line by line
cout << Symbol.at(count);
count++;
}
}
}
int DownloadHTML()
{
cout << "HTML Downloaded";
}
int main(int argc, char** argv) {
cout << "Name your Index: ";
cin >> Index;
ReadIndexFile();
DownloadHTML();
return 0;
}
As you can clearly see in the error message that "Boost" library not found.
ld: library not found for -lboost
So you need to install it using the following command;
sudo apt-get install libboost-all-dev
Hope this helps.
Edit:
As MAC does not support apt-get so you need to use http://brew.sh/.
Please have a look in this url http://stackoverflow.com/questions/19688424/why-is-apt-get-function-not-working-in-terminal-on-mac-osx-10-9 for more details about how Homebrew.

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

SDL2 Install problems: makefile.win file not recognized: file format not recognized and collect2.exe [error] id return1 exit status

After I learned the very basics of cpp i decided to push myself ahead and try SDL2 and try to make a game. I found the lazy foo's SDL tutorials. I tried to follow it but i seemed to have problems with installation. After putting in a "test" code i tried compiling it, and these messages showed up on the log:
C:\File\Location\For\My\Project\Makefile.win file not recognized: File format not recognized
C:\File\Location\For\My\Project\collect2.exe [Error] ld returned 1 exit status
I think it might be a linking error and heres my linkers:-lmingw32-lSDL2main-lSDL2
I tried deleting this Makefile.win but the same message just showed up and there isn't even a collect2.exe
I'm using the Orwell Dev-C++ using the mingw gcc 4.8.1 32bit release compiler, and heres the code:
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char **argv){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}