473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C2C++ itch

Vio
While converting a C file to C++
the C line:
THREAD_CREATE(tid, (void *)server_thread, sss);

complains with:
ANSI C++ forbids implicit conversion from `void *' in argument passing.

some context info:

the macro THREAD_CREATE is:
#define THREAD_CREATE(tid, entry, arg) do { _beginthread((entry), 0,
(arg));\
(tid) = GetCurrentThreadId(); \
} while (0)
#endif

server_thread is:
void THREAD_CC server_thread(void *arg){...}

Someone suggested to make it explicit, but I don't know how to do that
right now (or perhaps I don't understand what he ment). Declaring
server_thread explicit doesn't work, since that only works for
constructors (which server_thread is not).

Other suggestions?

Cheers,
Vio

Jul 22 '05 #1
8 2005

"Vio" <vm*******@sympatico.ca> wrote in message news:41**************@sympatico.ca...
While converting a C file to C++
the C line:
THREAD_CREATE(tid, (void *)server_thread, sss);

It's not even valid in C. The arg to beginthread isn't a void*, it's a pointer
to a function. Server_thread is also a pointer to function. First try, get
rid of the the (void*) cast.
Jul 22 '05 #2
Vio
Ron Natalie wrote:
"Vio" <vm*******@sympatico.ca> wrote in message news:41**************@sympatico.ca...
While converting a C file to C++
the C line:
THREAD_CREATE(tid, (void *)server_thread, sss);


It's not even valid in C. The arg to beginthread isn't a void*, it's a pointer
to a function. Server_thread is also a pointer to function. First try, get
rid of the the (void*) cast.



Hello Ron, thx for your reply.

strange this is illegal C, as it comes from an official O'Reilly book:
http://www.opensslbook.com/NSwO-1.3.tar.gz
in file ssl/server.c

Yeah, essentially I am trying to move that file to c++, in order to
add some c++ facilities, like fstream, std::string and such.

Getting rid of the (void *) cast.
You mean something like:
THREAD_CREATE(tid, server_thread, sss);

Complains with:
myserver.cpp:334: passing `void (*)(void *)' as argument 3 of
`pthread_create(pthread_t *, const pthread_attr_t *, void * (*)(void *),
void *)'

Ok, I'm kind of lost here. To make it more understandable,
I expanded the macro to:

//THREAD_CREATE(tid, (void *)server_thread, ssl);
do { _beginthread(((void *)server_thread), 0, (ssl));
(tid) = GetCurrentThreadId();
} while (0)

"_beginthread()", well, I don't know where it's coming from
(grep-ing some include dirs returned empty), but anyways

"server_thread" is a function in server.c
The (void *) cast seems needed by gcc, as it compiles ok.
g++ chokes with the forementionned whining.

Ok, I'm pretty lost here (well, I am just a newbie after all).
More hints appreciated :)

Cheers,
Vio

Jul 22 '05 #3
Vio wrote:

While converting a C file to C++
the C line:
THREAD_CREATE(tid, (void *)server_thread, sss);

complains with:
ANSI C++ forbids implicit conversion from `void *' in argument passing.

some context info:

the macro THREAD_CREATE is:
#define THREAD_CREATE(tid, entry, arg) do { _beginthread((entry), 0,
(arg));\
(tid) = GetCurrentThreadId(); \
} while (0)
#endif

server_thread is:
void THREAD_CC server_thread(void *arg){...}


Well, my guess is that you are compiling this under an OS
other than MS Windows. If this is so, then your assumption about
the macro expansion is incorrect. From your other post it
appears that you are actually using pthreads. In particular,
THREAD_CREATE uses pthread_create() rather than _beginthread().
pthread_create() expects

void* server_thread(void* arg); // (note the return value type)

for the "entry" parameter. Make sure that you've got the
platform-specific parts right, double-check the function
signatures, and you should be able to simply write

THREAD_CREATE(tid, server_thread, sss);

Denis
Jul 22 '05 #4
Vio
Denis Remezov wrote:
Well, my guess is that you are compiling this under an OS
other than MS Windows. If this is so, then your assumption about
the macro expansion is incorrect. From your other post it
appears that you are actually using pthreads. In particular,
THREAD_CREATE uses pthread_create() rather than _beginthread().
pthread_create() expects

void* server_thread(void* arg); // (note the return value type)

for the "entry" parameter. Make sure that you've got the
platform-specific parts right, double-check the function
signatures, and you should be able to simply write

THREAD_CREATE(tid, server_thread, sss);

Denis


Hi Denis,
Thanks for your reply.
Yes, I am using pthreads, and my OS is Debian (linux).

Oops, you got me here!! For some reason, I completely missed that
#ifdef. You are completely right, I expanded the win32 macro instead of
the pthread one. Let's call it channel vision :(
Ok, I'll correct that, and see what happens.

Many thanks for pointing that out,
Vio

Jul 22 '05 #5
Vio
Denis Remezov wrote:
Well, my guess is that you are compiling this under an OS
other than MS Windows. If this is so, then your assumption about
the macro expansion is incorrect. From your other post it
appears that you are actually using pthreads. In particular,
THREAD_CREATE uses pthread_create() rather than _beginthread().
pthread_create() expects

void* server_thread(void* arg); // (note the return value type)

for the "entry" parameter. Make sure that you've got the
platform-specific parts right, double-check the function
signatures, and you should be able to simply write

THREAD_CREATE(tid, server_thread, ssl);

Ok, let's see now:

The linux-version of the expanded macro is:

pthread_create(&(tid), NULL, ((void *)server_thread), (ssl));

which complains as initially noticed:
myserver.cpp:360: ANSI C++ forbids implicit conversion from `void *' in
argument passing
What compiler expects is actually (from "pthread.h"):
extern int pthread_create (pthread_t *__restrict __thread,
__const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROW;

Hm, I guess my problem is that I don't quite understand the 3rd
argument. I mean, really, what does

void *(*__start_routine) (void *)

actually mean? Well, I could make some guesses, but I don't want to
scare people who might read this :)
Well, just to elliminate some obvious possibilities,

pthread_create(&(tid), NULL, server_thread, (ssl));

myserver.cpp:369: passing `void (*)(void *)' as argument 3 of
`pthread_create(pthread_t *, const pthread_attr_t *, void * (*)(void *),
void *)'

Hm, so I'm essentially missing an asterix. So developping story:
if arg `server_thread' is `void (*)(void *)',
how do I make it into a `void * (*)(void *)' ?

Vio

Jul 22 '05 #6
Vio wrote:
[...] Hm, so I'm essentially missing an asterix. So developping story:
if arg `server_thread' is `void (*)(void *)',
how do I make it into a `void * (*)(void *)' ?


You are passing the address of the wrong function. Type
void* (*)(void*) means "pointer to a function taking void* and
returning void*". That means you have to use

void* server_thread(void* arg) {...}

instead of

void server_thread(void* arg) {...}
As a side note, in a perfect world I would also indicate the proper
language linkage:
extern "C" void* server_thread(void* arg) {...}
though a bare "C++" function will probably have the same calling
convenvtions anyway. OTOH, in the MS world you would have to use
the calling conventions specification THREAD_CC instead:
void* THREAD_CC server_thread(void* arg) {...}

Denis
Jul 22 '05 #7
Vio
Denis Remezof and Ron Natalie, many thanks for your help.
Essentially, doing

Denis Remezov wrote:
void* THREAD_CC server_thread(void* arg) {...}


gives warning:

myserver.cpp:302: warning: control reaches end of non-void function
`server_thread(void *)'

but it compiles, and I can live with that :)
Hm, some unwanted "undefined reference" during linking:

g++ -g -o myserver myserver.o reentrant.o common.o -L/d/ssl/run/lib
-lssl -lcrypto -Wall -pthread

but I've been there before, so I'll eventually figure this one out.
Just wondering, is there some special requirements when linking
against C object code from cpp? reentrant.o and common.o are C code,
which I didn't touch (my cpp code includes common.h which includes
reentrant.h). I recall something similar for the client code (did that
some weeks ago), and my fix, if I recall, was to put all functions
from all these 3 files in the same cpp file
(then did some editing there, put some as class methods,
while other remained C-like -without the `extern "C"' statement).

Anyway, many thanks,
Vio

Jul 22 '05 #8
Vio
Vio wrote:
Hm, some unwanted "undefined reference" during linking:

g++ -g -o myserver myserver.o reentrant.o common.o -L/d/ssl/run/lib
-lssl -lcrypto -Wall -pthread

but I've been there before, so I'll eventually figure this one out.
Just wondering, is there some special requirements when linking
against C object code from cpp? reentrant.o and common.o are C code,
which I didn't touch (my cpp code includes common.h which includes
reentrant.h). I recall something similar for the client code (did that
some weeks ago), and my fix, if I recall, was to put all functions
from all these 3 files in the same cpp file
(then did some editing there, put some as class methods,
while other remained C-like -without the `extern "C"' statement).

took me a little to figure it out, but embedding the C functions
in `extern "C"{...}' fixed that.
Vio

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ken | last post by:
How do I call c:\wget\wget.exe from a php or html file? so I can use php variables to define the file names to download. I want to set up wget to download several files from a server to my hard...
0
by: Manoj Nair | last post by:
I am doing a build on Unix box and deploying it as WAR. Till yesterday my XML to PDF was working till the point of invoking the acrobat reader. But since this morning I have started getting this...
0
by: Global Infotech Corporation | last post by:
Hi We have the following two positions open at one of our Client's site in the Silicon Valley, CA area. Our client, apart from being a pioneer in desktop publishing software provider, also makes...
1
by: singh.c2c | last post by:
HI, Dear business partners My name is B. Seshu narayana singh I worked with abc incorporation since the past two years hence for i request you to please work on my requirements and send me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.