LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   G++ environment variables, rerouting output, file listings and PIDs (https://www.linuxquestions.org/questions/programming-9/g-environment-variables-rerouting-output-file-listings-and-pids-364126/)

Kawahee 09-17-2005 02:54 AM

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.

Matir 09-17-2005 08:38 PM

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'.

Kawahee 09-18-2005 02:58 AM

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.

Matir 09-18-2005 11:51 AM

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.

Kawahee 09-18-2005 07:17 PM

Thanks for that. I don't suppose you know which environment variable g++ sets? Maybe a common/standard one, like $ERROR or $ERRNO?

Matir 09-18-2005 07:19 PM

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");


Kawahee 09-19-2005 04:14 AM

Fair enough. Thanks for that.

Matir 09-19-2005 12:40 PM

No problem, always glad to help.


All times are GMT -5. The time now is 10:37 PM.