MinGW无法生成.a?

a_b_c_ 2007-09-04 08:30:49
尝试如下:
1 hellos.h文件中:
void printS(char* str);

2 hellos.c文件中:

#include "hellos.h"

void printS(char* str)
{
printf("print in static way: %s", str);
}

3 在MinGW中输入
gcc -c -o hellos.o hellos.c
ar cqs libhellos.a hellos.o
想生成一个libhellos.a库,却出现了如下的错误提示:

new@MICROSOF-ACBC06 /c/msys/local
$ cd /f/asdf

new@MICROSOF-ACBC06 /f/asdf
$ gcc -c -o hellos.o hellos.c
In file included from hellos.c:1:
hellos.h:6:7: warning: no newline at end of file
hellos.c:6:2: warning: no newline at end of file

new@MICROSOF-ACBC06 /f/asdf
$ ar cps libhellos.a hellos.o
libhellos.a: No such file or directory
C:\msys\mingw\bin\ar.exe:
new@MICROSOF-ACBC06 /f/asdf
$
怎么会这样?
...全文
243 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_b_c_ 2007-09-04
  • 打赏
  • 举报
回复
解决了,原来-L后面少了个点!!!晕死!!
a_b_c_ 2007-09-04
  • 打赏
  • 举报
回复
生成libhellos.c之后再建立文件:
/* main.c */
#include <stdio.h>
#include "hellos.h"

void main()
{
char* text = "Hello World!\n";
printS(text);
}

在MinGW执行:
new@MICROSOF-ACBC06 /f/asdf
$ gcc -o hello main.c -L -lhellos
main.c: In function `main':
main.c:5: warning: return type of 'main' is not `int'
main.c:8:2: warning: no newline at end of file
C:/DOCUME~1/new/LOCALS~1/Temp/ccYFbaaa.o:main.c:(.text+0x38): undefined reference to `printS'
collect2: ld returned 1 exit status
竟然提示找不到printS这个函数?!!!!!
星羽 2007-09-04
  • 打赏
  • 举报
回复
gcc -c HelloWorld.c -o HelloWorld.o


ar cqs libHelloWorld.a HelloWorld.o
wang_wuhui 2007-09-04
  • 打赏
  • 举报
回复
-s说错了..
wang_wuhui 2007-09-04
  • 打赏
  • 举报
回复
-c 好像可以生成.a的文件..
MSVC vs. MinGW 之 (lib,dll,def,obj,exe) vs (a,dll,def,o,exe) 玩转攻略手记 一份粗糙的研究记录,有待补完和整理。 MinGW: c -> o gcc -c a.c c -> exe gcc a.c libs.o -o a.exe (从主程序a.c,附加libs,生成a.exe) o -> exe gcc a.o b.o ... -o main.exe c -> dll,def,a gcc a.c -shared -o a.dll -Wl,--output-def,a.def,--out-implib,liba.a a -> dll a2dll liba.a dll -> a: dlltool --dllname a.dll --def a.def --output-lib liba.a (需要def文件) a -> def: dumpbin /exports lib.a > lib.def (在windows上调用,def需要修改) dll -> def : pexports a.dll -o > a.def (这里的-o是指给函数标序号) lib -> def : reimp -d a.lib lib -> a: (for __cdecl functions in most case) reimp a.lib; (for __stdcall functions) MSVC: c -> lib cl /LD a.c (注意已经定义了export列表) c -> dll cl /LD a.c c -> obj cl /c a.c c -> exe cl a.c /out:a.exe dll ->lib lib /machine:ix86 /def:a.def /out:a.lib (需要def文件) obj ->lib lib a.obj b.obj... /out:mylib.lib dll ->def DUMPBIN a.dll /EXPORTS /OUT:a.def (生成的def需要做修正) lib ->def reimp -d a.lib (这个要在MSYS+MinGW下用) 关于这些工具的适用范围可以很容易的理解和记忆。 dll和exe都是PE文件,所以可以使用pexports. lib和a是静态库文件,都是归档类型,不是PE格式。所以不能使用pexports. dll可以使用dlltool. lib可以使用lib, 和reimp(lib->a工具) 所有的bin文件,包括dll,exe,lib,a都可以使用dumpbin. 参考: http://hi.baidu.com/kaien_space/blog/item/5e77fafa2ba9ff16a8d3110a.html Mingw官网文档: http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs http://oldwiki.mingw.org/index.php/CreateImportLibraries http://www.mingw.org/wiki/FAQ http://hi.baidu.com/opaquefog/blog/item/9b21b6deb324e25dccbf1ab7.html http://qzone.qq.com/blog/8330936-1238659272 http://hi.baidu.com/jzinfo/blog/item/b0aa1d308de99f9da8018e00.html 本篇测试用代码: 1. main.cpp #include #include #include "mylib.h" using namespace std; int main() { char str[]="Hello world!"; printhello(str); return 0; } 2. mylib.cpp #include #include #include "mylib.h" using namespace std; void EXPORT printhello(char *str) { cout << str << endl; } 3. mylib.h #define EXPORT __declspec(

70,041

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧