can't get my code to run from a programming book(c++) - multithreading

i got a new programing book (multicore programming by cameron hughes, tracey hughes).
so far i have not got one of their programs to work their book says that it should work on 99% of computers so im a little confused but at the end of each program in their book they have "compile and link instructions"... do i need to enter that? it looks something like this "C++ -o guess_it guess_it.cc". the code im runnning right now is:
#include <iostream>
#include <windows.h>
#include <string>
#include <spawn.h>
#include <sys/wait.h>
using namespace std;
int main(int argc,char *argv[],char *envp[])
{
pid_t ChildProcess;
pid_t ChildProcess2;
int RetCode1;
int RetCode2;
int Value;
RetCode1 = posix_spawn(&ChildProcess,"find_code",NULL,
NULL,argv,envp);
RetCode2 = posix_spawn(&ChildProcess2,"find_code",NULL,
NULL,argv,envp);
wait(&Value);
wait(&Value);
return(0);
}
im running windows 7(32-bit), AMD athion x2 7550 dual-core proessor, VC++ 2008 Express edition.
i get the following error : fatal error C1083: Cannot open include file: 'spawn.h': No such file or directory
anyone know why i can't get my code to run? do i need to download something? because i read the book and did not see anything about downloading anything but i might be wrong. :(

It looks like that book is using POSIX threading. Visual Studio uses Windows Threading by default, which has a completely different API.
You most likely just need to get a copy of a POSIX Thread library for Windows. That will include spawn.h and the appropriate lib files for you to use.

Forgive me if I'm misreading your level of experience here, but it sounds as though you are a complete beginner with this language.
The example compilation and link instruction in the book
C++ -o guess_it guess_it.cc
is an example of how to invoke a compiler and linker from the command line. If you're using Visaul C++ then the IDE will automate the compilation and link process for you when you click the "build" button, so you don't need to worry about doing this from the command line.
On to the error you're seeing in VC++:
The compiler is telling you that it can't find the header file spawn.h, which you've told it that your program needs in the line
#include <spawn.h>
As other on this page have mentioned, spawn.h is a file supplied by the POSIX standard libraries and contains functionality for spawning new processes.
Respectfully, it sounds to me from the way you asked your question ("compile and link instructions") as though you don't really understand what you're doing. Before you delve into multi-threading in C++, I recommend taking a step back and find a beginner's book on C++ using Visual Studio, and start from the beginning. I'm afraid you'll make very little progress unless you take the time to learn the fundamentals, and using the compiler is about as fundamental as it gets!
Good luck!

Related

error: ‘Object’ does not name a type - trying to learn NodeJS N-API

Having trouble finding a sufficiently precise answer, basically my compiler (GCC std=c++11) does recognise the type "Object".
Yes, I am fairly new to ++
Tried changing command line options, tried Googling for an exact solution.
If anyone spots any other potential issues, please do let me know. I am finding the NodeJS docs a bit ambiguous in some areas.
I have included my code and command line output.
Command line snippet:
g++ exampleMod.c -I/usr/include/c++/4.8.5 -I/lhome/psyjw21/node-v10.16.3-linux-x64/include/node -I /usr/include/c++/4.8.5/x86_64-redhat-linux -std=c++11
exampleMod.c:7:1: error: ‘Object’ does not name a type
Object obj = Object::New(env);
The code:
#define NAPI_VERSION 3
#include <node.h>
#include <node_buffer.h>
#include <node_version.h>
#include <node_object_wrap.h>
Object obj = Object::New(env);
obj["foo"] = String::New(env, "bar");
Never mind - I have found the problem.
I included what was meant to be part of the Javascript in my C++ file.
obj["foo"] - I kind of realised what I did when I saw I had used a string as an array index... that isn't going to work in C++! (actually, I need to check a few things because I didn't think you could do it Javascript either).
Anyway, yes, feel free to comment on what a dumb mistake this was....

Building R Package that uses RcppArmadillo, RcppEigen and depends on Cpp11 Plugins

I followed all the procedures explained so far about this matter either in this website or published notes by Dirk, Hadley or others. However, I still have problems in building my package due to the issue regarding cpp11 plugin.
I used RcppArmadillo.package.skeleton() function. I put my cpp file in the src directory. The NAMESPACE file looks as it should which contains importFrom(Rcpp, sourceCpp) line. I also edited DESCRIPTION file and in the LinkingTo section, I added RcppEigen and other packages I use. I finally ran the compileAttributes(verbose=TRUE) function in R and everything looked OK. Therefore, I think I have done everything as I should. I have to also mention that when I compile my code in R using sourceCpp(), it works perfect and is compiled with no errors!
To illustrate better what my dependencies are, I put the first block of my code here:
#include <RcppArmadillo.h>
#include <RcppNumerical.h>
#include <RcppArmadilloExtensions/sample.h>
#include <Eigen/LU>
#include <algorithm>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppNumerical)]]
// [[Rcpp::plugins(cpp11)]]
The problem is when I build my package and I get errors and warnings for the lines I have auto type which relates to cpp11 plugin.
After searching similar posts on this website, I concluded that I have to force my R compiler to use c++11 and there fore I edited my Makvars file located at ~/.R/Makevars and since I use MAC I added this line:
CXX=clang++ -std=c++11 to that file. However, when I do that those 3 errors go away but 50 new errors are generated as all of the Armadillo variable types, such as mat, uvec, etc are not recognized any more. So I don't know how to fix this.
I think basically putting // [[Rcpp::plugins(cpp11)]] should take care of it as the new version of Rcpp supports this plug in and probably that's why when I run sourceCpp in R I get no errors and everything looks fine. But I don't know what happens when building my package. My Rcpp version is 0.12.8 .
Thank you in advance for any sorts of help.
Plugins for both dependencies (ie other headers) and compiler options are for use by sourceCpp().
Packages do this with LinkingTo: and, for the C++11 directive, either src/Makevars or SystemRequirements. See Writing R Extensions which documents this.

Inquiry: Integrating libsndfile with Visual Studio 2010 C++. Error: libsndfile.dll not found

I am teaching myself how to read in wav files into C++ as a part of me learning C++. I have found many resources online that recommended the following library: libsnfile library
So I followed some tutorials below in testing the basic functionality of the library, but I can't get the library to compile with Visual Studio 2010.
I have searched online for the following error, but did not find anything useful for my particular error. I downloaded the libsndfile C++ windows installer found here. I used the 32bit version since I am using the win32 C++ console version. However, my Visual Studio is 64 bit. I did the following after I downloaded the installer:
I went into Visual Studio. Under my project, I did the following:
In project properties:
1. VC++
Include >> added ...\libsnfile\include
Library >> added ...\libsnfile\lib
2. C\C++
Added the following directory as additional dependencies
...\libsnfile\lib\libsndfile-1.lib
I did this to add this third party library to my project. After this, to test, I ran the following code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("This is a test\n");
getchar();
return 0;
}
I coded that to make sure that I could access the sndfile.h in my program and everything compiled. The problem occured when I tried to implement the following code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("This is a test\n");
//This will be the length of the buffer used to hold samples while the program processes them.
//A SNDFILE is like FILE in a standard C library. Consequently, the sf_open_read and sf_open_write functions will return an
//SNDFILE* pointer when they successfully open the specified file.
SNDFILE* sf = NULL;
/*SF_INFO will obtain information of the file we wish to load into our program. */
SF_INFO info;
/*This is where the program will open the WAV file */
info.format = 0;
sf = sf_open("C:\Users\GeekyOmega\Desktop\gameon.wav", SFM_READ, &info);
if(sf == NULL)
{
printf("Failed to open the file.\n");
exit(-1);
}
getchar();
return 0;
}
I then get a system error when I click run inside visual studio when I try to run my program. It says,
The program can't start because libsnfile-1.dll is missing from your computer.
Try reinstalling the program to fix this problem.`
I tried the 64 bit windows installer and tried that, but it didn't work. Anyone understand what I am doing run? I am running Visual Studio's 2010 on Windows 7 as my dev environment.
I apologize if I am making a silly mistake, but I would deeply appreciate if anyone could help me. I tried a few hacky fixes, as I talked about above, but nothing has worked.
EDIT: I am also aware of this thread here, but this doesn't make any sense to my current issue as I am not doing any of this path stuff that they are talking about.
Warm Regards,
GeekyOmega
I fixed the issue. For future readers, this is a very common problem, I think. I placed the .dll in the debug folder of my Visual Studio project. Visual Studio couldn't see the .dll file otherwise. After this, the program fired up as expected and ran. If this does not fix the issue for you, then I suggest something else could be going on.

Getting Vim to syntax-check boost libraries

I am a fairly novice programmer who recently started using boost. After successfully linking the libraries with cmake, I have noticed that my vim (syntastic plugin I think) which does a great job at highlighting syntax errors. But ever since i started including boost libraries, it just stops at the #include statement with (no such file / directory ) and fails to show up any syntax errors whatsoever in the rest of the file. I have search all over the place but I am unable to find a workaround which allows me to syntax check bad code prior to the compilation stage. any help will be appreciated.
I am unable to post screenshots (too low rating) but will post code for whatever it is worth
#include <iostream>
#include <boost/regex.hpp> <--------------syntax error (though it compiles fine)
#include <algorithm>
using namespace std;
void testMatch(const boost::regex& ex,const string st){
cout<<"Matching" <<st <<endl;
if(boost::regex_match(ex,st)){
cout<<"matches"<<endl
}
else cout<<"oops"; }
void testSearch(const boost::regex& ex, const string st){
cout<<"Searching"<<endl;
}
If you are using the Syntastic plugin, take a look at the file in
syntastic/syntax_checkers/cpp.vim
there are lots of language specific options that can be set, I think the one you'll want is
let g:syntastic_cpp_include_dirs=['path/to/boost/files']
this lets the sytax checker know that there are other places to look for included files besides the default ones.

Standard Template Library using g++

While migrating a program from windows in linux I encountered a problem using the c++ standard template library. I am trying to typedef a template and I am getting the error 'expected initializer before '<' token on this line
typedef std::list< std::pair< int,double> > PairList;
Any ideas why this would work using mvc++ and not using g++ and how I can fix it?
I think this is about #includes.
The following really minimal piece of code compiles perfectly here with g++ on Linux
#include <utility>
#include <list>
typedef std::list< std::pair< int,double> > PairList;
PairList x;
One thing to remember about standard include files is that they are allowed but not required to call each other. (It's not like they're potentially polluting the namespace by this, since they all use namespace std, which you aren't supposed to mess with.)
It is possible that, in MSVC++, includes , or vice versa, but this is not the case in the g++ headers. Therefore, a program might compile in MSVC++ and not in g++, with a required header missing in the source.
Make sure all of your required headers are actually included, and you should be fine.
Did you #include <utility> for pair?
I have had no problems with the code in G++, and generally found its STL support to be superb. Do you have all the #include directives there? Sometimes those differ from platform to platform (even when they shouldn't).

Resources