I can't get this to compile! I have no clue what I did wrong Sad

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

using namespace std;

pid_t pid;
pid = fork(); //Clone to make a child process
if(pid < 0) //If our PID is less than 0, then something has gone wrong while forking
  exit(EXIT_FAILURE);
if(pid > 0) //If out PID is greater than 0, then we're all good
  exit(EXIT_SUCCESS);

/* If we have gotten this far we are in the child process now */
umask(0); //Set the usermask to 0
openlog(argv[0],LOG_NOWAIT|LOG_PID,LOG_USER); //Connect to the syslog server
syslog(LOG_NOTICE, "Successfully started skeletor backend.\n");

pid_t sid;
sid = setsid(); //Let's try and create our own process group
if (sid < 0) {
  syslog(LOG_ERR, "Could not create process group.\n");
  exit(EXIT_FAILURE);
 }

if((chdir("/")) < 0){
  syslog(LOG_ERR, "Could not change working directory to /\n");
  exit(EXIT_FAILURE);
 }

/* Close the standard file descriptors as not to waste memory and for safety */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

/* Here is where the main loop of the daemon is. */
syslog(LOG_NOTICE, "Test 123! I am exiting now!\n");

Also, here is the makefile if it helps.

Code:
CC=gcc
CFLAGS=-I.
DEPS =
OBJ = main.o
EXE = skeletor

%.o: %.c $(DEPS)
   $(CC) -c -o $(EXE) $< $(CFLAGS)

hellomake: $(OBJ)
   gcc -o $@ $^ $(CFLAGS)

Edit: Here is the compilation error

Code:
g++    -c -o main.o main.cpp
main.cpp:8:1: error: ‘pid’ does not name a type
main.cpp:9:1: error: expected unqualified-id before ‘if’
main.cpp:11:1: error: expected unqualified-id before ‘if’
main.cpp:15:6: error: expected constructor, destructor, or type conversion before ‘(’ token
main.cpp:16:8: error: expected constructor, destructor, or type conversion before ‘(’ token
main.cpp:17:7: error: expected constructor, destructor, or type conversion before ‘(’ token
main.cpp:20:1: error: ‘sid’ does not name a type
main.cpp:21:1: error: expected unqualified-id before ‘if’
main.cpp:26:1: error: expected unqualified-id before ‘if’
main.cpp:32:6: error: expected constructor, destructor, or type conversion before ‘(’ token
main.cpp:33:6: error: expected constructor, destructor, or type conversion before ‘(’ token
main.cpp:34:6: error: expected constructor, destructor, or type conversion before ‘(’ token
main.cpp:37:7: error: expected constructor, destructor, or type conversion before ‘(’ token
make: *** [main.o] Error 1
More errors! WOOOOOOOO
My code
Code:

#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <syslog.h>
#include <fstream>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
pid_t pid;
pid = fork(); //Clone to make a child process
if(pid < 0) //If our PID is less than 0, then something has gone wrong while forking
  exit(EXIT_FAILURE);
if(pid > 0) //If out PID is greater than 0, then we're all good
  exit(EXIT_SUCCESS);

/* If we have gotten this far we are in the child process now */
umask(0); //Set the usermask to 0
openlog(argv[0],LOG_NOWAIT|LOG_PID,LOG_USER); //Connect to the syslog server
syslog(LOG_NOTICE, "Successfully started skeletor backend.\n");
 ofstream dmesg;
 dmesg.open("/dev/kmesg");
 dmesg << "Skeletor-0.1.0 started.";
 dmesg.close();

pid_t sid;
sid = setsid(); //Let's try and create our own process group
if (sid < 0) {
  syslog(LOG_ERR, "Could not create process group.\n");
  exit(EXIT_FAILURE);
 }

if((chdir("/")) < 0){
  syslog(LOG_ERR, "Could not change working directory to /\n");
  exit(EXIT_FAILURE);
 }

/* Close the standard file descriptors as not to waste memory and for safety */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

/* Here is where the main loop of the daemon is. */
 syslog(LOG_NOTICE, "Test 123! I am exiting now!\n");
}


The error

Code:

main.o: In function `main':
main.cpp:(.text+0x8a): undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream()'
main.cpp:(.text+0xb6): undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)'
main.cpp:(.text+0xca): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.cpp:(.text+0xd6): undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::close()'
main.cpp:(.text+0x183): undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream()'
main.cpp:(.text+0x198): undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream()'
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x1ca): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x1e1): undefined reference to `std::ios_base::Init::~Init()'
main.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
make: *** [skeletor] Error 1
Those are all linking errors; the very first Google result (this one) for those errors tells you the solution:
StackOverflow wrote:
gcc main.cpp -lstdc++

or

g++ main.cpp
Did you bother Googling the errors?
Stack overflow is blocked here :/
Thanks though!
Edit: That didn't work...
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement