Can't run make using C++ stdlib system call - c++

I've got the following code in C++
if (should_run_make) {
std::string make = "make -C ";
make.append(outdir);
std::cout << "Make cmd is " << make << std::endl;
system(make.c_str());
}
This reports the following:
Make cmd is make -C /home/hamiltont/temp/ make: Entering directory
/home/hamiltont/temp' make: *** No targets. Stop.
make: Leaving directory/home/hamiltont/temp'
However, doing it manually works fine in multiple ways e.g.
[hamiltont#4 generator]$ make -C /home/hamiltont/temp/
make: Entering directory `/home/hamiltont/temp'
g++ -O3 -I/usr/include/openmpi-x86_64 -L/usr/local/lib -L/usr/lib64/openmpi/lib -lmpi -lmpi_cxx -lboost_serialization -lboost_mpi stg_impl.cpp -o impl
make: Leaving directory `/home/hamiltont/temp'
[hamiltont#4 generator]$ cd /home/hamiltont/temp/
[hamiltont#4 temp]$ make
g++ -O3 -I/usr/include/openmpi-x86_64 -L/usr/local/lib -L/usr/lib64/openmpi/lib -lmpi -lmpi_cxx -lboost_serialization -lboost_mpi stg_impl.cpp -o impl

Are you generating the makefile from within your C program? That's the only reason I could imagine would cause that specific error message.
make: *** No targets. Stop.
Reproducing the error
Here's how I could generate that message:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("Makefile", "w");
fputs("all:\n\techo Done.\n", fp);
system("make");
fclose(fp);
return 0;
}
This, predictably, prints:
make: *** No targets. Stop.
I say predictably because Makefile will be empty! This is because IO is buffered...
Fixed version
So, I close the file before calling system(), which flushes the buffer (fflush() would also do the trick):
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("Makefile", "w");
fputs("all:\n\techo Done.\n", fp);
fclose(fp);
system("make");
return 0;
}
Output:
echo Done.
Done.
I used C's IO functions for clarity, but the same rules apply to <iostream>.

Related

using c++ Boost in a makefile

I have a simple C++ program which I compile with clang using the Boost library and with C++14 support.
I use the following command to compile my sample.cpp file and it works fine:
clang++ -g -std=c++1y -I$BOOST_ROOT sample.cpp -o sample
where $BOOST_ROOT is the path to where I downloaded and extracted the boost zip file.
$BOOST_ROOT=/usr/local/boost_1_66_0/
When I try to compile the same sample.cpp file with a makefile, it doesn't work.
This is what my makefile looks like:
sample: sample.cpp
clang++ -g -std=c++1y -I$BOOST_ROOT sample.cpp -o sample
Running make command, I get the error:
Sample.cpp:9:10: fatal error: 'boost/format.hpp' file not found
#include <boost/format.hpp>
^~~~~~~~~~~~~~~~~~
1 error generated.
And here is the sample.cpp
//
// sample.cpp
//
//
#include <iostream>
#include <boost/format.hpp>
using namespace std;
int main()
{
std::cout << "Enter your first name: " << std::endl;
std::string firstName;
std::cin >> firstName;
std::cout << "Enter your surname: " << std::endl;
std::string surname;
std::cin >> surname;
auto formattedName = str( boost::format("%1% %2%"s) % firstName % surname );
std::cout << "You said your name is: " << formattedName << std::endl;
return 0;
}
CONSOLE OUTPUT
MacBook-Air:Listing_1_7 userd43f$ make
c++ sample.cpp -o sample
sample.cpp:9:10: fatal error: 'boost/format.hpp' file not found
#include <boost/format.hpp>
^~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [sample] Error 1
MacBook-Air:Listing_1_7 userd43f$ ls $BOOST_ROOT
INSTALL boost boost.png bootstrap.sh index.html rst.css
Jamroot boost-build.jam boostcpp.jam doc libs status
LICENSE_1_0.txt boost.css bootstrap.bat index.htm more tools
The problem was that there were spaces instead of a tab in the command-line of my makefile.
BOOST_ROOT := /usr/local/boost_1_66_0
BOOST_INC := ${BOOST_ROOT}/include
sample: sample.cpp
clang++ -g -std=c++1y -I$(BOOST_ROOT) sample.cpp -o sample
In the last line just before clang++ command I was using spaces instead of a TAB. I replaced all the spaces with a single TAB before clang++, and also I needed to put the BOOST_ROOT inside the parentheses as (mentioned by #MaximEgorushkin)
Then it started picking up the right command, as shown in the output below:
MacBook-Air:Listing_1_7 userd43f$ make
clang++ -g -std=c++1y -I/usr/local/boost_1_66_0 sample.cpp -o sample
Boost root directory normally has include and lib directories in it.
It should probably be:
BOOST_ROOT := /usr/local/boost_1_66_0
BOOST_INC := ${BOOST_ROOT}/include
sample: sample.cpp
clang++ -g -std=c++1y -I${BOOST_INC} sample.cpp -o sample

Error when using fstream in Netbeans/MingW

A while ago I installed MingW to learn C++ with NetBeans, but until now I didn't find the time. Now I created a new C++ project, made a Hello World program with std::cout and it worked immediately. Then I tried to write a file, but it failed:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
cout << "test" << endl;
fstream f;
f.open("a.txt", fstream::in | fstream::out | fstream::trunc);
f << "out" << endl;
f.close();
return 0;
}
lumosc (Build, run):
cd 'D:\<My name>\Programmierung\NetBeans Projects\lumosc_1\lumosc'
D:\Programme\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/D/Programme/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
"/D/Programme/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW_1-Windows/lumosc.exe
make.exe[2]: Entering directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
mkdir -p build/Debug/MinGW_1-Windows
rm -f "build/Debug/MinGW_1-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/MinGW_1-Windows/main.o.d" -o build/Debug/MinGW_1-Windows/main.o main.cpp
mkdir -p dist/Debug/MinGW_1-Windows
g++ -o dist/Debug/MinGW_1-Windows/lumosc build/Debug/MinGW_1-Windows/main.o
make.exe[2]: Leaving directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
make.exe[1]: Leaving directory `/d/<My name>/Programmierung/NetBeans Projects/lumosc_1/lumosc'
Output (Note that cout << "test" << endl; wasn't executed):
RUN FAILED (exit value -1.073.741.511, total time: 64ms)
I didn't see any error message. However, when I started the newly created lumosc.exe manually, the following window popped up:
(Translated: Entry point not found - The procedure entry point "__gxx_personality_v0" was not found in the "libstdc++-6.dll" DLL.)
Here's some information about my MingW installation:
I'm completely new to this topic and I hope somebody can help me out!

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.

cpp-netlib complains about missing lboost-thread

I'm learning cpp-netlib and I tried running the exmaple client given on the official website. The code is very simple:
#include <boost/network/protocol/http/client.hpp>
#include <iostream>
int main(int argc, char *argv[]) {
using namespace boost::network;
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " [url]" << std::endl;
return 1;
}
http::client client;
http::client::request request(argv[1]);
request << header("Connection", "close");
http::client::response response = client.get(request);
std::cout << body(response) << std::endl;
return 0;
}
And here is my makefile for this c++ application:
CC = g++ -std=c++11
CFLAG = -I/usr/local/Cellar/boost/1.57.0/include
LIBFLAG = -L/usr/local/Cellar/boost/1.57.0/lib
all: client
client: client.o
$(CC) $(LIBFLAG) -lboost_system -lboost_thread client.o -o client
client.o: client.cpp
$(CC) -c $(CFLAG) client.cpp
clean:
rm -rf *.o client
It complains about not finding lboost_thread library after compilation:
ld: library not found for -lboost_thread
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [client] Error 1
In my boost library directory, the boost_thread library shows up like this:
libboost_thread-mt.a libboost_thread-mt.dylib
Why isn't it able to find this library? Did I make any mistake in the linking?
Try changing your makefile to link to -lboost-thread-mt instead of -lboost-thread.
You seems to be missing libboost_thread for some reason

How to link to shared lib in c++

Could someone help me with linking to a shared lib, specifically libzmq, in C++?
all: clean compile
clean:
rm bin *.o -f
compile:
g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin
I've installed libzmq using the following steps:
git clone https://github.com/zeromq/libzmq.git
cd libzmq
./autogen.sh
./configure
make && sudo make install
Here's my main.cpp
#include <iostream>
#include <string>
#include <zmq/zmq.h>
// Required by fork routine
#include <sys/types.h>
#include <unistd.h>
// Required by wait routine
#include <sys/wait.h>
#include <stdlib.h> // Declaration for exit()
#include <cstdio> // printf
using namespace std;
int global_variable = 2;
int main(int argc, char** argv){
const short int FORK_FAILED = -1;
const short int FORK_SUCCESS = 0;
int stack_variable = 20;
pid_t pid;
string status_identifier;
switch (pid = fork()){
case FORK_SUCCESS:
printf("Child changing global and stack variables\n");
global_variable++;
stack_variable++;
break;
case FORK_FAILED:
cerr << "Failed! -- Failed to fork: " << pid << endl;
exit(1);
default:
printf("Child process (pid=%d) created successfully.\n", pid);
wait(0);
break;
}
printf("[pid=%d] Global: %d\n", pid, global_variable);
printf("[pid=%d] Stack: %d\n", pid, stack_variable);
return 0;
}
And, here's the error msg:
bitcycle # ubuntu64vm ~/git/test $ make
rm bin *.o -f
g++ -g -Wall -I/usr/local/include -L/usr/local/lib main.cpp -lzmq -o bin
main.cpp:4:23: fatal error: zmq/zmq.hpp: No such file or directory
compilation terminated.
make: *** [compile] Error 1
The error is pretty straight forward, but I've yet to find a solution. Any ideas?
My goal is to do something like this with multiple child processes.
Update I'm just going to install it system-wide in ubuntu: sudo apt-get install libzmq-dev, and that resolved the issue. It doesn't teach me anything about how to identify a shared lib and header file on disk and link to it... but I guess I can move that to another day.
C++ wrapper for ZeroMQ (zmq.hpp) is no longer part of ZeroMQ. There is no zmq.hpp in current libzmq master or in latest stable 3.2.x.