Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 112627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:34:28+00:00 2026-05-11T02:34:28+00:00

I’m trying to compile the following simple DL library example code from Program-Library-HOWTO with

  • 0

I’m trying to compile the following simple DL library example code from Program-Library-HOWTO with g++. This is just an example so I can learn how to use and write shared libraries. The real code for the library I’m developing will be written in C++.

#include <stdlib.h> #include <stdio.h> #include <dlfcn.h>  int main(int argc, char **argv) {     void *handle;     double (*cosine)(double);     char *error;      handle = dlopen ('/lib/libm.so.6', RTLD_LAZY);     if (!handle) {         fputs (dlerror(), stderr);         exit(1);     }      cosine = dlsym(handle, 'cos');     if ((error = dlerror()) != NULL)  {         fputs(error, stderr);         exit(1);     }      printf ('%f\n', (*cosine)(2.0));     dlclose(handle); } 

If I compile the program with gcc it works fine.

gcc -o foo foo.c -ldl 

When I change the filename and compiler to the following

g++ -o foo foo.cpp -ldl 

I get the following error:

foo.cpp:16: error: invalid conversion from ‘void*’ to ‘double (*)(double)’

I understand (I think I understand, correct me if this is wrong) that I can’t do an implicit cast from a void pointer in C++, but C lets me, and this is why the above code will compile using gcc but not using g++. So I tried an explicit cast by changing line 16 above to:

cosine = (double *)dlsym(handle, 'cos'); 

With this in place, I get the following error:

foo.cpp:16: error: cannot convert ‘double*’ to ‘double (*)(double)’ in assignment

These problems probably have more to do with my own general ignorance of proper C++ coding standards than anything else. Can anyone point me to a good tutorial on developing dynamic libraries for Linux that uses C++ example code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:34:28+00:00Added an answer on May 11, 2026 at 2:34 am

    C allows implicit casts from void * to any pointer type (including function pointers); C++ requires explicit casting. As leiflundgren says, you need to cast the return value of dlsym() to the function pointer type you need.

    Many people find C’s function pointer syntax awkward. One common pattern is to typedef the function pointer:

    typedef double (*cosine_func_ptr)(double); 

    You can define your function pointer variable cosine as a member of your type:

    cosine_func_ptr cosine; 

    And cast using the type instead of the awkward function pointer syntax:

    cosine = (cosine_func_ptr)dlsym(handle, 'cos'); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Iain posted a solution to using mock objects with AuthLogic.… May 11, 2026 at 9:25 pm
  • Editorial Team
    Editorial Team added an answer If Core Data WERE available on the iPhone, you could… May 11, 2026 at 9:25 pm
  • Editorial Team
    Editorial Team added an answer I managed to find a way to do it: $('div.editbox').bind('keydown',… May 11, 2026 at 9:25 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.