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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:34:40+00:00 2026-05-27T04:34:40+00:00

I am just getting started with gcc on Linux. I am following the tutorial

  • 0

I am just getting started with gcc on Linux. I am following the tutorial here, except that I am using the g++ compiler.

hello_fn.cpp

#include <stdio.h>
#include "hello.h"

void 
hello (const char * name)
{
  printf ("Hello, %s!\n", name);
}

bye_fn.cpp

#include <stdio.h>
#include "hello.h"

void 
bye (void)
{
  printf ("Goodbye!\n");
}

hello.h

void hello (const char * name);
void bye (void);

I then run the following in the shell:

$ g++ -Wall -c hello_fn.cpp
$ g++ -Wall -c bye_fn.cpp
$ ar cr libhello.a hello_fn.o bye_fn.o

Then I try the following from python:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes 
>>> test = ctypes.cdll.LoadLibrary(r'/home/oob/development/libtest/libhello.a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/ctypes/__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/jeff/development/libtest/libhello.a: invalid ELF header

My idea was to write some functions in c++ and call them from Python. Any ideas?

UPDATE: I was able to get things “working”. Based on what Cat Plus Plus said, I may not go this direction for new code, but I was able to get this to work with a large legacy c++ library that I was porting from Windows to Linux. We need a frontend to call some long running functions from this library, so I thought Python might be easiest. The functions create a lot of output and only return an integer return code, so maybe I can avoid the “painful” stuff Cat Plus Plus was saying.

Here is what I did.

Modified hello_fn.cpp

#include <stdio.h>
#include "hello.h"

extern "C" int 
hello (void)
{
  return 16;
}                                                                                

Modified by_fn.cpp

#include <stdio.h>
#include "hello.h"

extern "C" void 
bye (void)
{
  printf ("Goodbye!\n");
}

Modified hello.h

extern "C" int hello (void);
extern "C" void bye (void);

buildscript.sh

#!/bin/bash

rm *.o
rm *.so

g++ -fpic -g -c -Wall hello_fn.cpp
g++ -fpic -g -c -Wall bye_fn.cpp
#make a shared library, not a static library (thanks cat plus plus)
g++ -shared -o libhello.so hello_fn.o bye_fn.o

test.py

#!/usr/bin/python

import ctypes

c = ctypes.cdll.LoadLibrary(r'/home/jeff/development/libtest/libhello.so')
a = c.hello()
print 'hello was ' + str(a)
c.bye()

Try it in the terminal….

oob@ubuntu:~/development/libtest$ ./build_script.sh 
oob@ubuntu:~/development/libtest$ python test.py 
hello was 16
Goodbye!

Our legacy library doesn’t really use any windows-specific c++ stuff (thanks to the guy who wrote that code), so it has been a pretty easy port. We had several functions that used extern “C” to expose functions. For the port, I have made the following changes:

#ifdef LINUX
#define __stdcall
#endif
#ifdef WINDOWS
#define __stdcall __stdcall
#endif

And for one of our functions, I can just leave it unchanged, for example:

extern "C" long __stdcall reform_proj {
    //do a bunch of stuff
    return 0;
}
  • 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-05-27T04:34:40+00:00Added an answer on May 27, 2026 at 4:34 am

    ctypes is for loading shared libraries. ar creates archives of object files, also known as static libraries. You can’t load that file with ctypes, it will only be understood by the linker.

    Another issue is that using C++ shared libraries via ctypes is painful if not downright impossible. Just don’t. Use Cython instead, and write a proper Python extension that interfaces with your C++ code (then you can link it either statically or dynamically, and it’ll work).

    Another option is Boost.Python, but it’s somewhat less documented, but has a benefit of defining Python module directly in your C++ code, instead of using wrappers written in another language.

    Third is SWIG, but I’ve never used it, so can’t tell you how well it works in practice.

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

Sidebar

Related Questions

I'm just getting started with CruiseControl .NET (using the Manning Continuous integration book that
Just getting started using MVC in ASP.NET, I'm going to have it so users
Just getting started with C++ here. I am working on OSX with Eclipse CDT.
I am just getting started with flex and am using the SDK (not Flex
I'm just getting started with Boost for the first time, details: I'm using Visual
Just getting started with OpenFrameworks and I'm trying to do something that should be
Just getting started with XPath, and using it's implementation with PHP's SimpleXML objects. Right
im just getting started on maven, and im currently following the article from http://www.sonatype.com/books/mvnex-book/reference/simple-project-sect-create-simple.html
Just getting started with Obj-C and iOS programming. I have some code that loads
I am just getting started using any DI/IoC toolset and have a very basic

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.