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 8159449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:55:06+00:00 2026-06-06T17:55:06+00:00

My CMake setting to create a shared lib in linux is something like SET

  • 0

My CMake setting to create a shared lib in linux is something like

SET (CMAKE_CXX_FLAGS "-fPIC")

SET (LIB_UTILS_SRC
    Utils.cpp
)

ADD_LIBRARY (UTILS SHARED
    ${LIB_UTILS_SRC}
)

Source Utils.cpp

double addTwoNumber(double x, double y)
{
    return x + y;
}

When trying to access ‘addTwoNumber’ function using CTypes like

import os
import ctypes as c

libPath = '/home/AP/workspace/LearningCPP/lib/libUTILS.so'
libUTILS = c.cdll.LoadLibrary(libPath)

prototype = c.CFUNCTYPE(    
    c.c_double,                
    c.c_double,                
    c.c_double                
)
addTwoNumber = prototype(('addTwoNumber', libUTILS))

res = addTwoNumber(c.c_double(2.3), c.c_double(3.5) )

I am getting some message like.

AttributeError: /home/AP/workspace/LearningCPP/lib/libUTILS.so:
undefined symbol: addTwoNumber

I checked the libUTILS.so using the “nm –demangle libUTILS.so” command and it clearly shows the ‘addTwoNumber’ symbol in it.

Why am I still getting the “undefined symbol” message from python ?
I am guessing there must be some compiler flags to be set so that symbols are mangle properly. Any suggestion would be appreciated !

  • 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. Editorial Team
    Editorial Team
    2026-06-06T17:55:08+00:00Added an answer on June 6, 2026 at 5:55 pm

    Interesting, I usually use numpy.ctypes since I constantly have to deal with large data sets, and never had any issues but I think I know whats going on here, it’s that the names are being mangled by the g++ compiler, I made it work this way:

    Makefile:

    g++ -Wall -fPIC -O2 -c Utils.cpp
    g++ -shared -Wl -o libUTILS.so Utils.o
    

    Utils.cpp

    extern "C" double addTwoNumber(double x, double y)
    {
        return x + y;
    }
    

    test.py

    import os
    import ctypes as c
    
    libUTILS = c.cdll.LoadLibrary('libUTILS.so')
    
    prototype = c.CFUNCTYPE(    
        c.c_double,                
        c.c_double,                
        c.c_double                
    )
    addTwoNumber = prototype(('addTwoNumber', libUTILS))
    
    res = addTwoNumber(c.c_double(2.3), c.c_double(3.5) )
    print res
    

    output:

    $ python test.py
    5.8
    

    note the extern keyword this makes sure the compiler doesn’t mangle the name, you have to do some extra stuff when under windows, I did find http://wolfprojects.altervista.org/dllforpyinc.php which was kind of interesting.

    I hope this helps.

    my machine:

    $ g++ –version
    i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
    Copyright (C) 2007 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    $ uname -a
    Darwin MacBookPro 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried setting the debug flags using the set command in cmake but
CMake generates something like the following for the install rule: # Special rule for
I writing toolchain.cmake file for Linaro toolchain. include(CMakeForceCompiler) set(CMAKE_CROSSCOMPILING TRUE) set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_VERSION 1)
I would like to use the Intel compiler icc (or icpc) with a CMake-based
I'm setting up my visual studio project to use CMake, but I got two
In CMake, I want to create a directory if it doesn't already exist. How
CMake offers several ways to specify the source files for a target. One is
The download page for cmake only shows a 32 bit installer for windows. Any
I am building a cmake project that includes cuda code. I am unable to
When I use CMake with FIND_PACKAGE( OpenCV REQUIRED ) , all components of OpenCV

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.