This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Very specific C++ compilation syntax error that I can't figure out a solution to: I'm fairly new to C++ and read up on functions being passed as parameters. The compile error doesn't make sense to me because I've read the code over-and-over. Please help.
Edit: I removed the waitpid(pid_t,int,int) and stuck with just system() commands. Thanks for the help everyone.
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(){
/* puts apt-get purge text into ~$ purge_e_output */
system("sudo apt-get purge enlightenment > purge_e_output.txt");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
system("echo **REMOVING ENGLIGHTENMENT**");
system("sudo apt-get purge enlightenment");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* puts apt-get autoremove text into ~$ autoremove_e_output */
system("sudo apt-get autoremove > autoremove_e_output.txt");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
system("echo **REMOVING E DATA**");
system("sudo apt-get autoremove");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* puts apt-get autoclean text into ~$ autoclean_e_output */
system("sudo apt-get autoclean > autoclean_e_output.txt");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
system("echo **CLEANING**");
system("sudo apt-get autoclean");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* reinstall englightenment */
system("echo **REINSTALLING**");
system("sudo apt-get install enlightenment");
system("echo **Pid of apt-get**");
system("pidof apt-get");
try{
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
} catch(...){}
/* logs */
system("echo && echo Logs for wtf just happened:");
system("echo && echo ~$ purge_e_output.txt && echo ~$ autoremove_e_output.txt && echo ~$ autoclean_e_output.txt");
}
Compile error:
~$ g++ JIC.cpp -o JIC
JIC.cpp: In function ‘int main()’:
JIC.cpp:12: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:18: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:26: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:34: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:42: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:49: error: expected ‘,’ or ‘...’ before ‘(’ token
JIC.cpp:58: error: expected ‘,’ or ‘...’ before ‘(’ token
This doesn't make sense in that place:
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
You cannot do this
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
instead you need to actually create instances of the variables that you want to pass to this function. For example
pid_t pid = getpid();
int status = // set to some value that you choose
then you can call your function like this
pid_t pid2 = waitpid(pid, &status, WUNTRACED);
now pid2 is the value returned by the waitpid function.
I would strongly advise you to do a little more reading - whether it be books or tutorials - as your question shows you don't yet have a strong grasp of the language. I mean absolutely no offence by that at all - just trying to help.
The line
pid_t waitpid(pid_t getpid("apt-get"), int *statusPtr, int WUNTRACED);
looks like a function declaration, both to me and to the C++ compiler. It looks like you're trying to declare a function named waitpid whose return type is pid_t, which takes three parameters, a pid_t, an int*, and an int. However, the name of a parameter can't be getpid("apt-get"), so besides this being the wrong place in the file for a function declaration, you can't interpret this as a function declaration because it tries to use a function call as the name of a parameter.
Conversely, if you try to interpret this line as a function call to a function named waitpid (that's already defined), getpid("apt-get") makes sense as a parameter because it means you want to pass the result of the getpid function as the first parameter to the waitpid function. However, the rest of that line is incorrect syntax for a function call, because the types of the parameters to a function call should not be specified inline - you should be passing it parameters that have already been defined as variables or functions. A syntactically-correct call to the waitpid function might look like this:
int* statusPtr = ...;
int WUNTRACED = ...;
pid_t myPid = waitpid(getpid("apt-get"), &statusPtr, WUNTRACED);
And the following definition might appear elsewhere:
pid_t waitpid(pid_t pid, int *statusPtr, int WUNTRACED) {
...
}
Related
I am testing the example code posted on the official libtorrent website (https://www.libtorrent.org/tutorial-ref.html). I pasted the code here:
#include <libtorrent/session.hpp>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/torrent_handle.hpp>
#include <libtorrent/magnet_uri.hpp>
int main(int argc, char const* argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s <magnet-url>\n");
return 1;
}
lt::session ses;
lt::add_torrent_params atp = lt::parse_magnet_uri(argv[1]);
atp.save_path = "."; // save in current dir
lt::torrent_handle h = ses.add_torrent(atp);
// ...
}
I have already installed the libtorrent:
ldconfig -v | grep libtorrent
libtorrent-rasterbar.so.9 -\> libtorrent-rasterbar.so.9.0.0
I used the following command to compile the code:
g++ main.cpp -o run -ltorrent-rasterbar -lboost_filesystem-mt
However, I got errors:
main.cpp: In function 'int main(int, const char\*\*)':
main.cpp:12:3: error: 'lt' has not been declared
lt::session ses;
I also saw another solution, but it does not resolve the issue I am facing: How to compile a libtorrent(rasterbar) code ?
Does anyone know what caused this failure?
I'm trying to run a graphics program on my Ubuntu 18.04 LTS system to print the error code for failed graphics operation. My code is
#include <graphics.h>
#include <stdlib.h>
int main()
{
int gd, gm, errorcode;
initgraph(&gd, &gm, NULL);
errorcode = graphresult();
if(errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to exit.");
getch();
exit(1);
}
getch();
closegraph();
return 0;
}
But when I run it I get the following error :
g++ -o mygraphics mygraphics.c -lgraph
mygraphics.c: In function ‘int main()’:
mygraphics.c:10:20: error: ‘graphresult’ was not declared in this scope
errorcode = graphresult();
^~~~~~~~~~~
mygraphics.c:12:24: error: ‘grOk’ was not declared in this scope
if(errorcode != grOk)
^~~~
mygraphics.c:12:24: note: suggested alternative: ‘brk’
if(errorcode != grOk)
^~~~
brk
mygraphics.c:14:42: error: ‘grapherrormsg’ was not declared in this scope
printf("Graphics error: %s\n", grapherrormsg(errorcode));
I searched all over the internet but not able to find a promising solution. Can someone help me out please. Thank you in advance :)
For an assignment, i am supposed to implement the linux terminal. My terminal should support arguments with pipes. Like the user can input:
ls | grep ".cpp"
What i have done so far is:
pid = fork();
if(pid==0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp("/bin/ls","ls");
}
wait(NULL);
pid=fork();
if(pid==0)
{
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp("/bin/grep","grep",".cpp");
}
my first child works perfectly, writes the output of ls into a pipe declared earlier, however, my second child runs grep, but it apparently can not get input from pipe. Why is that so?
when i run it, my output is
/root/OS $ ls | grep
it just gets stuck like this
Don't ignore compiler warnings:
$ gcc lol.c
lol.c: In function ‘main’:
lol.c:14:5: warning: not enough variable arguments to fit a sentinel [-Wformat=]
execlp("/bin/ls","ls");
^
lol.c:23:5: warning: missing sentinel in function call [-Wformat=]
execlp("/bin/grep","grep",".cpp");
^
Here's man execlp:
The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL
pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
Always compile with -Werror so that compilation fails if there are warnings, and preferably also with -Wall to get warnings for all potential issues.
Here's your program with a main method and sentinels added:
#include <unistd.h>
#include <sys/wait.h>
int main() {
int fd[2];
int pid;
pipe(fd);
pid = fork();
if(pid==0)
{
close(fd[0]);
dup2(fd[1],1);
close(fd[1]);
execlp("/bin/ls","ls", NULL);
}
wait(NULL);
pid=fork();
if(pid==0)
{
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp("/bin/grep","grep",".cpp", NULL);
}
return 0;
}
Here's how it runs now:
$ gcc -Wall -Werror lol.c -o lol
$ ./lol
foo.cpp
I am trying to run rsync through execvp with StrictHostKeyChecking option.
This is my code:
#include <unistd.h>
int main() {
char *argv[] = {"rsync",
"remote_user#1.2.4.5:/tmp",
"/home/tmp/",
"-e 'ssh -o StrictHostKeyChecking=no'",
0};
execvp("rsync", argv);
}
I am getting this error:
rsync: -e '-o StrictHostKeyChecking=no': unknown option
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]
I have tried another way:
#include <unistd.h>
int main() {
char *argv[] = {"rsync",
"remote_user#1.2.4.5:/tmp",
"/home/tmp/",
"-e",
"'ssh -o StrictHostKeyChecking=no'",
0};
execvp("rsync", argv);
}
But now it is failing with error:
rsync: Failed to exec ssh -o StrictHostKeyChecking=no: No such file or directory (2) rsync error: error in IPC code (code 14) at pipe.c(84) [sender=3.0.6]
Why it don't understand StrictHostKeyChecking option?
rsync expects to receive the options first, followed by the hosts. You're doing it backwards: first you need to specify -e and -o.
You also shouldn't be single-quoting the -o option: that is needed when invoking it from bash, to prevent bash from interpreting the arguments and splitting them into separate argv[] entries. Think about it: when bash sees '-o StrictHostKeyChecking=no', it passes -o StrictHostKeyChecking=no as a single argv[] entry, without the single quotes (because the single quotes is your way to tell the shell that you don't want argument splitting).
Last, but not least, you should check that execvp(3) didn't fail.
So, this is what you need:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
char *argv[] = {
"rsync",
"-e",
"ssh -o StrictHostKeyChecking=no",
"remote_user#1.2.4.5:/tmp",
"/home/tmp/",
NULL
};
if (execvp("rsync", argv) < 0) {
perror("rsync(1) error");
exit(EXIT_FAILURE);
}
return 0;
}
I want to wirte a function with variable arguments in this way:
static void configElement(U32 localFaultId,
char* name,
U32 report,
U32 localId,
U32 detectTime,
U32 ceaseTime,...)
{
U32 i = 0;
U32 tmpNo = 0;
va_list ap;
if (nofFaults >= MAX_NOF_LOCAL_FAULTS)
{
//something here
return;
}
else
{
faultList[nofFaults].ceaseTime = ceaseTime;
va_start(ap, ceaseTime);
tmpNo = va_arg(ap, U32);
while ((tmpNo!= END_MARK) && (i < MAX_NOF_DEPEND))
{
faultList[nofFaults].dependList[i++].faultNo = tmpNo;
}
faultList[nofFaults].dependList[i].faultNo = END_MARK;
/* Finish by increment nofFaults parameter */
va_end(ap);
nofFaults++;
}
}
However, I got the error msg when compiling this code:
fault_manager.cc:3344: error: expected primary-expression before ',' token
fault_manager.cc:3387: error: expected primary-expression before 'U32'
fault_manager.cc:3387: error: expected `)' before 'U32'
fault_manager.cc:3387: error: expected `)' before ';' token
fault_manager.cc:3387: error: expected `)' before ';' token
I have no idea what is going wrong here. My platform is Windows, and I'm using cygwin+Eclipse(CDT). The version of gcc is 4.1.1.
Any idea will be appreciated much!
It looks like the compiler does not know what U32 is. Did you include all necessary headers?