标签归档:Cygwin

Eclipse+CDT+Cygwin,让我情何以堪啊……

为了编写跨平台程序,一是采用了Eclipse+CDT+Cygwin开发,中间真是莫名其妙的问题层出不穷,每次解决了问题,不知道什么时候又回冒出来,而有些问题,第一次出现后记录下来的解决办法,第二次却无法依据同样的方法解决,真是让人欲哭无泪啊……

1、multiple target patterns问题

记得最先碰到这个问题,可以在项目的Properties的C/C++Builder—>Tool Chain Editor—>Current toolchain选择Cygwin,Current Builder由Gnu Maker Builder改为CDT Internal Builder解决。据说是因为Gun Maker的3.8.0版本有bug,改成3.8.1可以解决问题,当然可以选用CDT Internal Builder,也能解决这个问题。but,有时候,你用CDT Internal Builder,一样产生multiple target patterns问题,汗。。。

另一种说法是windows下的盘符后的冒号(如”D:\“)会被Maker解释成对象分隔符,从而导致Maker错误的认为后面是个编译对象,从而导致multiple target patterns问题,解决办法是在主菜单—>Window—>Preferences—>C/C++—>Debug—>Source Look Path中点击Add按钮,选择Path Mapping,添加盘符映射(如’C:\’映射成’\cygdrive\c\’、’D:\’映射成’\cygdrive\d\’)来解决。但貌似也不见得有效,汗。。。

2、同一个项目中的.cpp文件竟然找不到自己的.h文件

这个问题郁闷了我好几天,发现是因为一个项目引用另一个动态库项目时,如果通过Properties—>C/C++ General—>Path and Symbal—>References中勾选要引用的dll项目进行自动引用,用CDT Internal Builder则会出现同一个项目中的.cpp文件找不到自己的.h文件的问题,只好把CDT Internal Builder改成Gun Maker,然后再在Properties—>C/C++ Builder—>Settings的Includes和Libraries中手动加入对动态库的引用,即可解决问题。汗。。。

【转】Windows上调用Cygwin编译的函数库

2012-04-05 19:22
转载自 kaien_space

本篇介绍的是静态库函数。静态库扩展名是.lib(windows上)或.a(linux上),他和动态库(dll)是有区别的。

调用静态库编译后会写入执行程序中。然后就可以独立运行了。

动态库旨在动态调用,调用的时候需要加载dll才能正常工作。

(所以动态库往往可以提供补丁,或功能升级的时候使用,但是运行的速度有待商协)

另外,两个库的编译器也不一样,例如mingw用g++.exe生成动态库.dll, 用ar 生成静态库.a

而VC则一律用link.exe生成生成动态和静态库,用options来区别生成哪种。

 

本文只讲静态库.dll和.a 的例子。动态库其实很类似。

我们的目的是,把若干的C++和C文件在Cygwin上编译成一个静态库函数文件。

然后在Windows上调用这个库函数。

举个简单的例子:

我们有两个库函数文件.cpp或.c

1. myf1.cpp

#include <stdio.h>

void f1_Fonction1(int a, double b, char *c)
{
printf(“调用文件myf1.cpp的f1_Fonction1成功\n”);
}
int f1_Fonction2(int c)
{
printf(“调用文件myf1.cpp的f1_Fonction2成功\n”);
return c+1;
}

2. myf2.c

#include <stdio.h>

void f2_Fonction1(void)
{
printf(“调用文件myf2.c的f2_Fonction1成功\n”);
}
int f2_Fonction2(int c)
{
printf(“调用文件myf2.c的f2_Fonction2成功\n”);
return c+2;
}

上面两个文件一个是C++的,另一个是C的。

两个文件各提供了两个函数。

现在我们在Cygwin上编译他们为库函数mylib.a 或mylib.lib (扩展名不重要)

> gcc -c myf1.cpp myf2.c

由于我们这里的函数都是C标准格式,所以我们还使用g++, c++或cc等编译。选用任意一种都可以编译通过。

(通常情况下,选哪个编译器和你的C++代码的编写用语格式有关,每个编译器都有其特殊的支持格式)

上面的命令编译生成两个文件myf1.o和myf2.o

接着,我们使用ar生成静态库

> ar r mylib.lib myf1.o myf2.o

这样,我们就在cygwin上生成了一个静态库mylib.lib

现在,我们尝试在windows上调用这个库中的一个函数试试看

写一个简单的调用的C++程序main.cpp

#include <iostream>
using namespace std;

void f1_Fonction1(int a, double b, char *c);

int main()
{
char c;

f1_Fonction1(1,2.0,&c); //调用了myf1里面的函数f1_Fonction

cout << “Hello world!” << endl;
return 0;
}

现在,在window上用MinGW编译。

首先生成main.o文件

> gcc -c main.cpp

接着把main.o和函数库mylib.lib连接起来生成main.exe文件

> g++ -o main.exe main.o mylib.lib

注意link的顺序。函数库必须在主函数后面。另外,因为main.cpp是C++格式,所以link的时候要用g++编译器。gcc编译器会出现没有定义std::cout 这类的错误信息。

另外一个值得注意的是,虽然VC的.lib和Linux的.a格式是相同,都是一种归档文件格式。但是由于VC编译的程序中函数列表需要符号索引 (如果你用 reimp -s mylib.lib 察看VC的lib文件就会发现,函数名都被加了冗长的前后缀),所以vc的lib似乎无法直接在gcc上连接通过,同样的gcc的.a似乎也无法在vc上使用。虽然也有些文章讨论过lib和a的转换问题,配合使用reimp和dlltool命令转换。但是我的测试中发现转换后的库文件还是无法调用。如果有人成功的实现vc和gcc的静态库互换,请务必留言告诉我具体方法。

linux/cygwin下使用gdb三板斧

第一步,先用gdb装载exe程序的调试符

(我用的是cygwin,所以使用’/cygdrive/盘符’来跳转到I盘下的xxx/111目录下)

HYH@HYH-PC ~
$ cd /                                                                        <========到根目录

HYH@HYH-PC /
$ cd /cygdrive/i/SingleValueTrans/111                      <========到exe所在目录

HYH@HYH-PC /cygdrive/i/SingleValueTrans/111
$ gdb valuetrans.exe                                                <========用gdb装载exe程序的调试符
GNU gdb (GDB) 7.3.50.20111026-cvs (cygwin-special)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type “show copying”
and “show warranty” for details.
This GDB was configured as “i686-cygwin”.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>…
Reading symbols from /cygdrive/i/SingleValueTrans/111/valuetrans.exe…done.
(gdb)

 

第二步,用gdb运行程序
(gdb)
(gdb) r                          <========用gdb装载exe程序
Starting program: /cygdrive/i/SingleValueTrans/111/valuetrans.exe
[New Thread 22604.0x5de0]
[New Thread 22604.0x5e8c]
2012-06-07 18:17:04 Single value translation service is starting …

……

……

 

第三步,gdb已经自动在出问题的代码出停止并推出exe程序

2012-06-07 18:17:46 socket service listening at port ‘502’

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 22604.0×6030]
0x004e703d in sockServer::tListen (param=0x20074160) at ..\sockServer.cpp:193

轻松看到看到程序在 sockServer.cpp:193行出问题了

 

 

还有些招数,比如生成core文件,利用core文件调试等,很好很强大,但是上面三板斧已经能直接定位到问题所在了,杀鸡焉用牛刀呢

 

 

Shared libraries with Eclipse CDT and cygwin on Windows

Can you help me use shared libraries with Eclipse CDT, managed make and cygwin?“, I was asked yesterday. Read on for a list of common pitfalls and detailed instructions.

The instructions are based on the latest CDT release (Galileo) and cygwin (make 3.81, gcc 3.4.4). They are applicable to CDT’s managed make projects (that means CDT generates a makefile to build project).

The Pitfalls

It turns out that using a shared library on Windows is not as straight forward as you think. There are several pitfalls waiting for the unaware to fall into:

1. Recent versions of cygwin’s make insist on cygwin-style paths instead of windows paths (/cygdrive/c/foo instead of c:\foo). CDT is not picky about this and will generate an incorrect makefile, if you use workspace relative paths:

make all
example.d:1: *** multiple target patterns.  Stop.

The solution is to use absolute cygwin paths, such as: /cygdrive/c/workspace/mymath

2. The compiler and linker will not find the header files / library unless you set the appropriate parameters. The compiler needs an include path (-I). The linker needs the library name (-l) and library search path (-L). These settings are scattered in two places in the project properties. Their location is not obvious for a first-time user (details below).

3. When launching, Windows will not find the shared library (.dll) and greet you with the error pictured below. Unix users might try to set the LD_LIBRARY_PATH, which has no effect on Windows. The solution is to append the directory containing the .dll to the PATH (MSDN Article). Restart Eclipse for the changed PATH to take effect.

The remainder of the post walks you through the process of creating and using a simple shared library with cygwin and CDT.

Creating a Shared Library with CDT

Follow these instructions to create a shared library project with CDT.

1. File > New > Project > C Project > Next. Project name: mymath. Ensure “use default location” is checked. Note the location: c:\workspace\mymath — we’ll need it later. Project type: Shared Library; Empty Project. Hit Finish.

2. Create a header file (mymath.h) and the corresponding implementation (mymath.c). The example below provides a trivial function that multiplies two integers:

3. Afterwards save and hit Ctrl+B (or Project > Build All) to build the library. If cygwin is on your path, you should see a “Release” folder in your project containing the file “mymath.dll”.

4. For windows to find the shared library, you need to add the directory containing the .dll to your path. On Vista this can be done via: Control Panel > User Accounts > User Accounts > Change my environment variables.

5. Exit and restart Eclipse after changing the PATH. Otherwise the changes will not be picked up.

Using a Shared Library with CDT

Follow these instructions to use a shared library in a “managed make” CDT project.

1. File > New > Project > C Project > Next. Project name: example. Project type: Executable; Empty Project. Hit Finish.

2. In that project create a file named example.c with the following content:

3. Save and hit Ctrl+B to build the project. The second line will have an error: “mymath.h: No such file or directory”. We now have to adjust the compiler and linker settings so that the mymath.h / mymath.dll files are found during the build.

4. Select the “example” folder in the Project Explorer. Select “Project > Properties” from the menu. A dialog comes up. In the tree on the left open: “C/C++ General > Paths and Symbols”. In the “Languages” list, pick “GNU C”. Then hit “Add”. Enter the cygwin-style path to the “mymath” project: /cygdrive/c/workspace/mymath

Caution: When entering the path, don’t use the “Workspace” or “File system” buttons because the resulting path will not be compatible with cygwin’s make.

5. In the same dialog select: C/C++ Build > Settings in the tree on the left. In the “Tool Settings” tab find: “Cygwin C Linker > Libraries”. Hit the “+” icon in the “Libraries” section and add the name of the library: mymath

Caution: if your shared library starts with lib, omit the ‘lib’ prefix (i.e. libfoo becomes foo)

Hit the “+” icon in the “Library search path” section and add the path to the folder containing the shared library:/cygdrive/c/workspace/mymath/Debug

Hit OK.

6. You will be asked to rebuild the project. Answer “Yes”, but for some reason this will not rebuild your project. Hit Ctrl+B to rebuild. The error will go away.

Note: ignore the “unresolved inclusion” warning. It seems that CDT has trouble resolving cygwin-style paths. The generated make-file however will work as expected.

7. Select “example” in the Project Explorer. Right-click > Run As > Local C/C++ Application. At this point you see the result of the multiplication on the console. That means that the shared library was found and used successfully:

Kind regards,
Elias.

 

本文转自:http://eclipsesource.com/blogs/2010/03/03/shared-libraries-with-eclipse-cdt-and-cygwin-on-windows/

Linux到Windows的代码移植:Cygwin及MinGW简介

Linux系统向Windows移植,迟早你会碰到Cygwin或者MinGW。

Cygwin是许多自由软件的集合,最初由Cygnus Solutions开发,用于各种版本的Microsoft Windows上,运行UNIX类系统。Cygwin的主要目的是通过重新编译,将POSIX系统(例如Linux、BSD,以及其他Unix系统)上的软件移植到Windows上。Cygwin移植工作在Windows NT、Windows 2000、Windows XP以及Windows Server 2003上比较好,在Windows 95和Windows 98上,相对差劲一些。目前Cygwin由Red Hat等负责维护。

Cygwin包括了一套库,该库在Win32系统下实现了POSIX系统调用的API;还有一套GNU开发工具集(比如GCC、GDB),这样可以进行简单的软件开发;还有一些UNIX系统下的常见程序。2001年,新增了X Window System。

糟糕的是,Cygwin不支持Unicode。实际上,除了当前Windows系统以及OEM codepages(例如,一个俄语用户,他的codepages是CP1251和CP866,而不能是KOI8-R、ISO/IEC 8859-5、UTF-8等),Cygwin对其他字符集都不支持。

Red Hat规定,Cygwin库遵守GNU General Public License,但也可以跟符合开源定义的自由软件链接。Red Hat另有价格不菲的许可协议,这样使用Cygwin库的专属软件,就可以进行再发布。

MinGW(Minimalist GNU for Windows),又称Mingw32,是将GNU开发工具移植到Win32平台下的产物,包括一系列头文件(Win32API)、库和可执行文件。 MinGW是从Cygwin(1.3.3版)基础上发展而来,但是用MinGW使用Windows中的C运行库。因此用MinGW开发的程序不需要额外的第三方DLL支持就可以直接在Windows下运行,而且也不一定必须遵从GPL许可证。这同时造成了MinGW开发的程序只能使用Win32API和跨平台的第三方库,而缺少POSIX支持[1],大多数GNU软件无法在不修改源代码的情况下用MinGW编译。

MinGW的库可以跟Windows本地的MSVCRT库(Windows API)一起工作。MinGW占用内存、硬盘空间都比较少,能够链接到任意软件,但它对POSIX规范的实现没有Cygwin库完备。

以上信息大部分来自维基百科,更多信息敬请参阅:

http://zh.wikipedia.org/wiki/Cygwin

http://zh.wikipedia.org/wiki/MinGW