开发者

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

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜