LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-17-2005, 02:54 AM   #1
Kawahee
LQ Newbie
 
Registered: Sep 2005
Location: Queensland, Australia
Distribution: Windows
Posts: 4

Rep: Reputation: 0
G++ environment variables, rerouting output, file listings and PIDs


Hello all,

I've been tasked with writing a server for an online network my organisation is making, and we've been offered free Linux servers to run the finished program on. Even though I've typically stuck to developing for Windows/Winsock, developing a Linux Daemon/Sockets isn't all too different. C++ is C++, right? Anyway, my 'test' Linux box is a P166 with a whole 32 mb of RAM. It came shipped to me with with corrupted header files (think random stuff, like #defihe @@_$0DEATH), and the ability to segfault every third compile. I got a friend to recommend a distro for me and we upgraded to Trustix, and it worked fine. Being a P166, it doesn't have the speed required to run KDE or GNOME or a light desktop environment. I agreed to develop my code across SSH using nano with syntax highlighting, but called it quits after the first 2,000 lines. I asked my same friend what to do, and suggested that I run smb and a vpn, which I did do. The VPN was our work one, but with the Linux network connection bridged onto it. So now I had Visual Studio 2002 and a pretty much lag-free connection, even at home. The project has ballooned out to something like 22 cpp files, all of which I'm maintaining and still working on. I still had to initiate the compile process across SSH, but that was fine. I've experimented with make etc and had it work well. But now I want to start the compiling process from within VS .NET (call me lazy). I want to write an application that connects to a daemon running on port 666 that will ideally scan for all cpp files in /storm/src/cpp/*, compile them with g++, link them, send the results back to me and launch the daemon. I could easily do system ("make stormsvr");, but I figured while I'm at it I might as well learn something about Linux while I'm at it. At the moment, I need to:
[*]Learn how to reroute g++'s output to somewhere where I can echo it back to the Windows client[*]Learn how to list all the files in a directory and subdirectory[*]Check whether g++ produced an error. I take it that it sets an environment variable, but I don't know which one or how to read them from within C++[*]Find the PID of the currently running executable "stormsvr" and kill it

Any help would be appreciated.
 
Old 09-17-2005, 08:38 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
1. To reroute output you can do 'g++ ARGUMENTS file.cpp &> compiler_output' or something similar.
2. To list files, you can use ls. To make it recursive, add -R.
3. If g++ had an error, the environment variable "$?" is set to non-zero.
4. To just kill all currently running stormsvr's, you can use 'killall stormsvr'.
 
Old 09-18-2005, 02:58 AM   #3
Kawahee
LQ Newbie
 
Registered: Sep 2005
Location: Queensland, Australia
Distribution: Windows
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for the reply, but I don't mean from the console, I mean to list files through an API call. Windows has WIN32_FIND_DATA, Linux has ? And I also need to find out how to read environment variables from within my program, so when g++ fails, I can know about it and get the output and send it through the program.
 
Old 09-18-2005, 11:51 AM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
To test the return of a command (like g++) in a system call, you can just test the return of system(). Read the system manpage for details. Otherwise, environment variables can be read through getenv().

To read the list of files in a directory, you can use "readdir()" and similar calls.
 
Old 09-18-2005, 07:17 PM   #5
Kawahee
LQ Newbie
 
Registered: Sep 2005
Location: Queensland, Australia
Distribution: Windows
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks for that. I don't suppose you know which environment variable g++ sets? Maybe a common/standard one, like $ERROR or $ERRNO?
 
Old 09-18-2005, 07:19 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Generally, $? contains the status code of a program on return, or at least it does in the shell.

But, if you are using a system() call, you could always do:
Code:
int retval;

retval=system("g++ code.cpp");
if(retval != 0)
    fprintf(stderr,"An error occured with g++\n");
 
Old 09-19-2005, 04:14 AM   #7
Kawahee
LQ Newbie
 
Registered: Sep 2005
Location: Queensland, Australia
Distribution: Windows
Posts: 4

Original Poster
Rep: Reputation: 0
Fair enough. Thanks for that.
 
Old 09-19-2005, 12:40 PM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
No problem, always glad to help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to refer to environment variables in an ant file irfanhab Programming 1 09-12-2005 06:15 AM
environment variables nasht Linux - General 3 08-11-2005 02:12 PM
environment variables Prakhardeep Linux - Software 3 10-06-2004 03:30 AM
environment variables da Perp Linux - Newbie 5 01-23-2002 09:34 AM
environment variables aethereal Linux - Newbie 7 12-19-2001 09:34 AM


All times are GMT -5. The time now is 11:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration