CMake An example of compiling C + + DLLs (update 2: Adding Python call methods)

Source: Internet
Author: User


Project (XXX) add_library (xxx SHARED xxx.cpp) add_executable (yyy yyy.cpp) target_link_libraries (yyy XXX)


#ifndef Xxx_xxx_h#defineXxx_xxx_h#endif#pragmaOnce#ifdef Build_xxx_dll#defineIo_xxx_dll __declspec (export)#else#defineIo_xxx_dll __declspec (import)#endifextern "C"{Io_xxx_dllvoidHellovoid); Io_xxx_dllDoubleSumDoubleADoubleb);}


#defineBuild_xxx_dll#include"xxx.h"#include<iostream>using namespaceStd;io_xxx_dllvoidHellovoid) {cout<<"Hello from dll!\n"<<Endl;} Io_xxx_dllDoubleSumDoubleADoubleb) {cout<<"The sum of"<<a<<" and"<<b<<"is :"<<a + b<<Endl; returnA +b;}


#include <windows.h>#include<iostream>using namespacestd;intMain () {hinstance h= LoadLibrary ("c:\\users\\perelman\\. Clion2016.1\\system\\cmake\\generated\\xxx-4d5c076f\\4d5c076f\\debug\\libxxx.dll"); typedefvoid(*P0) (void); P0 Hello= (p0) GetProcAddress (H,"Hello"); cout<<"Run hello:\n"<<Endl;    Hello (); typedefDouble(*P1) (Double,Double); P1 sum= (p1) GetProcAddress (H,"sum"); cout<<"Run sum:\n"<<Endl; SUM (10.0,12.0);    FreeLibrary (h); return 0;}

ImportcTYPES#Note One, note the 64bit 32bit problem if a 193 error occurs:%1 is not a valid WIN32 application, see the compiler replaced by 64bit to 32bit#note Two, because the DLL is CDECL interface (such as extern "C" __declspec (dllimport)), so Python with cTYPES. Cdll Interfaceh = ctypes. Cdll ('c:\\users\\perelman\\. Clion2016.1\\system\\cmake\\generated\\xxx-4d5c076f\\4d5c076f\\debug\\libxxx.dll') H.hello ()#Note that the data in Python has to be ctypes processed .#Specify parameter formatH.sum.argtypes =(ctypes.c_double, ctypes.c_double)#indicates the return formatH.sum.restype =ctypes.c_doublePrint(H.sum (10.1, 3))

CMake An example of compiling C + + DLLs (update 2: Adding Python call methods)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.