Using a C library in eC (Both static and dll's)

General help with the core C language of which eC is a superset, integrating eC with other languages such as C++ at the C level, using C libraries in eC, etc.
Post Reply
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Using a C library in eC (Both static and dll's)

Post by jerome »

sanyaade asked:
I will be using Ecere both the Language and SDK with this group but I will also need to see how to be able to call external static or DLL C library from Ecere (static is more important), just in case the need arise.
I prepared an eC project (see attachment) to clearly show how this is done.

There is a second project for a sample C library (consisting of a single function).

Both projects have the regular Debug and Release project configurations, which use shared libraries (DLLs), and an additional configuration called 'Static' creating and linking in the static C library.

A few preprocessor definitions are defined in the Win32 project to control how the functions are exported and imported from the C library: USE_DLLEXPORT, USE_DLLIMPORT, USE_STDCALL.

USE_DLLEXPORT is defined when building the shared library, while USE_DLLIMPORT is defined when linking with it. USE_STDCALL is defined to standardize the function name mangling and calling convention ( a typical practice with Windows DLLs to use them across different compilers ).

As you can see, there is very little to using a library. You simply use #include to include the C library header (note: you can run into some conflicts doing this, where using #define / #undef can help, ask for help if you run into this).
Then you need sure the path to your C headers are included in your 'Additional include directories', within 'Project Settings / Compiler Settings'.
Then you add the name of the library to the 'Additional libraries' in Project Settings / Linker Settings, as well as the path to the built library (.lib or .dll for dynamic, .a for static) in 'Additional library directories'. Note that static library built with/for MinGW will always be preceded by the 'lib' prefix. That lib prefix should not be included when adding it to the 'Additional libraries' line: e.g. cLibrary even though the static library is libcLibrary.a .
For MSVC libraries (.lib), the full name should be included even if they are static libraries.

The attached zip file contains the projects as well as the built executables and workspace.
You can see in the eCproject workspace that the cLibrary project was added to the workspace as well. This will allow the IDE to automatically find and use the proper configuration DLL when debugging or running the application (even though it is located in a separate folder).

Let me know if you have any further questions about this!

Cheers,

Jerome
Attachments
cLibrary.zip
(86.34 KiB) Downloaded 6770 times
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Using a C library in eC (Both static and dll's)

Post by samsam598 »

It is the second time I encountered the same issue when using a C lib in eC project (both static and dll).Last time was to build a c container library,this time is to make use of curl lib.The issue is the same:when I tested both sample codes in pure c workspace (Code::Blocks +mingw for example),everything is perfect;but when I compiled the sample codes in eC,it complained "syntax error." from inside the code right begining to use the c lib.

Below is the project I test curl lib,the sample code void testCurl() just copied from the sample inside the distribute package.

Please help me to fix this issue(I mean things need to know when I have to talk with exsiting c libs),I wish I can use C to do everything that can do in other workspace can also do in eC.

Code: Select all

 
#ifdef __WIN32__
#define WIN32_LEAN_AND_MEAN 
 
#endif
 
#include <curl/curl.h>
 
import "ecere"
 
 
class App:Application
{
   void testCurl()
   {
 
     CURL* curl;
     CURLcode res;
     curl=curl_easy_init();
 
     if(curl)
     {
        curl_easy_setopt(curl,CURL_OPT_URL,"http://taobao.com");//@@@--->>>syntax error from here
        curl_easy_setopt(curl,CURL_OPT_FOLLOWLOCATION,1L);
        res=curl_easy_perform(curl);
        if(res!=CURLE_OK)
        {
           fprintf(stderr,"curl_easy_perform() failed :%s\n",
               curl_easy_strerr(res));
        }
     }
     curl_easy_cleanup(curl);
   }          
   void Main()
   {
     testCurl();
     system("pause");
   }
}
 
Attachments
testcurl.7z
(4.88 KiB) Downloaded 6824 times
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Using a C library in eC (Both static and dll's)

Post by jerome »

Hi Sam,

libcurl is doing some nasty non-C compatible stuff in its headers by default.
If you define CURL_DISABLE_TYPECHECK in the preprocessor definitions, it will avoid including typecheck-gcc.h which uses internal GCC things like __builtin_types_compatible_p which eC does not support. This is automatically disabled on non-GCC/older GCC and for C++ by a check in curl.h.

I managed to build and run successfully your code after I fixed a few other things:

- The CURLOPT_* do not have an underscore between CURL and OPT (CURLOPT_URL, CURLOPT_FOLLOWLOCATION)
- It is curl_easy_strerror(), not curl_easy_strerr()
- On Windows the libraries to link are "ecere curl eay32 ssleay32"

Hope this helps!

Regards,

Jerome
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Using a C library in eC (Both static and dll's)

Post by samsam598 »

Sorry for my carelessness!

CURL_DISABLE_TYPECHECK does matter!!With this preprocessor my code works!

BTW,just for your reference:for http://taobao.com and http://hao123.com things worked very differently:
http://taobao.com worked perfect in curl+ecere or curl+codeblocks+mingw,Chinese characters show correctly.

SmartFileOpen and all libararies based curl I have at hands(wxWidgets+wxCurl ,or curld+ D2 ,for example) or lNet with Lazarus(in freepascal) worked the same:can't recognize Chinese character;

http://hao123.com :wxWidget+wxCurl or lNet with Lazarus:in GUI TextBox,just show ?; curld+D2 or curl+ecere or curl+c+code::blocks: weird things happed,program hangs,BIOS keeps beeping,all mess unrecogizable characters(every single charater is unrecogizable)...
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Using a C library in eC (Both static and dll's)

Post by jerome »

Perhaps the two sites have different characters encoding? Remember eC/Ecere always expects UTF-8.
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Using a C library in eC (Both static and dll's)

Post by samsam598 »

www.taobao.com:

Code: Select all

<!doctype html>
<html>
<head>
<meta charset="gbk" />

www.hao123.com:

Code: Select all

<!DOCTYPE html><!--[if lt IE 7 ]><html class="ie6"><![endif]--><!--[if IE 7 ]><html class="ie7"><![endif]--><!--[if IE 8 ]><html class="ie8"><![endif]--><!--[if IE 9 ]><html class="ie9"><![endif]--><!--[if (gt IE 9)|!(IE)]><!--><html><!--<![endif]--><!--STATUS OK--><head><meta charset="utf-8"/>
www.yahoo.com:

Code: Select all

<!DOCTYPE html>
<html lang="en-US" class="dev-desktop uni-purple-border ua-ie ua-win ua-5.1 ua-ie8  bkt900  uni-dark-purple" style="">
<!-- m2 template  -->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Don't know whether this is useful for me to take reference.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Using a C library in eC (Both static and dll's)

Post by jerome »

Hi Sam,

Well from this it seems that the "gbk" one is the one that shows correctly.
But it could be your console where you're outputting it as well that's not set up in Unicode?
You could try to output to a text file and open it in the IDE to see what happens with both of these.

Regards,

Jerome
Post Reply