mingw命令行编译示例
来源:互联网 发布:mac手机地址怎么查 编辑:程序博客网 时间:2024/05/29 14:32
如果你正在学习c++ cookbook,那这篇文章对你会很有帮助
在阅读本文之前,请先看这篇文章
http://blog.csdn.net/dcmilan/article/details/7983697
1. 准备工作
1.1创建3个文件夹
johnpaul, georgeringo, hellobeatles
1.2加入源文件
根据注释提示的目录创建源文件
// johnpaul/john.hpp#ifndef JOHN_HPP_INCLUDED#define JOHN_HPP_INCLUDED void john( ); // Prints "John, " #endif // JOHN_HPP_INCLUDED---------------------------------------------// johnpaul/john.cpp#include <iostream>#include "john.hpp" void john( ){ std::cout << "John, ";}---------------------------------------------// johnpaul/paul.hpp#ifndef PAUL_HPP_INCLUDED#define PAUL_HPP_INCLUDED void paul( ); // Prints " Paul, " #endif // PAUL_HPP_INCLUDED---------------------------------------------// johnpaul/paul.cpp#include <iostream>#include "paul.hpp" void paul( ){ std::cout << "Paul, ";}---------------------------------------------// johnpaul/johnpaul.hpp#ifndef JOHNPAUL_HPP_INCLUDED#define JOHNPAUL_HPP_INCLUDED void johnpaul( ); // Prints "John, Paul, " #endif // JOHNPAUL_HPP_INCLUDED---------------------------------------------// johnpaul/johnpaul.cpp #include "john.hpp"#include "paul.hpp"#include "johnpaul.hpp" void johnpaul( ){ john( ); paul( );}---------------------------------------------// georgeringo/george.hpp#ifndef GEORGE_HPP_INCLUDED#define GEORGE_HPP_INCLUDED void george( ); // Prints "George, " #endif // GEORGE_HPP_INCLUDED---------------------------------------------// georgeringo/george.cpp#include <iostream>#include "george.hpp" void george( ){ std::cout << "George, ";}---------------------------------------------// georgeringo/ringo.hpp#ifndef RINGO_HPP_INCLUDED#define RINGO_HPP_INCLUDED void ringo( ); // Prints "and Ringo\n" #endif // RINGO_HPP_INCLUDED---------------------------------------------// georgeringo/ringo.cpp#include <iostream>#include "ringo.hpp" void ringo( ){ std::cout << "and Ringo\n";}---------------------------------------------// georgeringo/georgeringo.hpp#ifndef GEORGERINGO_HPP_INCLUDED#define GEORGERINGO_HPP_INCLUDED// define GEORGERINGO_DLL when building libgerogreringo.dll# if defined(_WIN32) && !defined(__GNUC__)# ifdef GEORGERINGO_DLL# define GEORGERINGO_DECL _ _declspec(dllexport)# else# define GEORGERINGO_DECL _ _declspec(dllimport)# endif# endif // WIN32#ifndef GEORGERINGO_DECL# define GEORGERINGO_DECL#endif// Prints "George, and Ringo\n"#ifdef __MWERKS__# pragma export on#endif GEORGERINGO_DECL void georgeringo( ); #ifdef __MWERKS__# pragma export off#endif#endif // GEORGERINGO_HPP_INCLUDED---------------------------------------------// georgeringo/ georgeringo.cpp#include "george.hpp"#include "ringo.hpp"#include "georgeringo.hpp" void georgeringo( ){ george( ); ringo( );}---------------------------------------------// hellobeatles/ hellobeatles.cpp#include "../johnpaul/johnpaul.hpp"#include "../georgeringo/georgeringo.hpp" int main( ){ // Prints "John, Paul, George, and Ringo\n" johnpaul( ); georgeringo( );}
2. 生成目标文件
在cmd下,进入每个cpp对应得目录运用g++命令
为每个cpp创建目标文件
命令格式:g++ -c –o john.o john.cpp或g++ -c john.cpp,这样就会默认生成与cpp同名的.o文件
一共有7个cpp,对应生成7个.o文件
3. 生成静态库
使用johnpaul文件夹中的文件生成静态库
命令格式为:ar ru libjohnpaul.a john.o paul.o johnpaul.o
生成的.a文件就是静态库
ar归档(archiver)的目的就是为了把多个.o文件打包,这样看起来更加清爽,也便于传播
4. 生成动态库
使用georgeringo文件夹中的文件生成动态库(dll)
命令格式为:g++ -shared –fPIC –o libgeorgeringo.so george.o ringo.o georgeringo.o
生成了一个.so文件,然后继续下面的命令(so代表shared object)
g++ -shared –o libgeorgeringo.dll Wl,--out-implib,libgeorgeringo.a –Wl,--export-all-symbols –Wl,--enable-auto-image-base george.o ringo.o georgeringo.o
这个生成的dll一会要放到生成的exe对应的文件夹里
5. 生成可执行文件
切换到hellobeatles文件夹
命令:g++ -o hellobeatles hellobeatles.o ../johnpaul/libjohnpaul.a ../georgeringo/libgeorgeringo.a
生成可执行文件hellobeatles.exe
将georgeringo文件夹中的dll拷贝到可执行文件的目录下,然后运行显示
John, Paul, George, and Ringo
6.完整执行过程
Microsoft Windows [版本 6.1.7600]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Users\ztn>cd D:\Exercise\CookExample\Chap1\beatles\johnpaul
C:\Users\ztn>d:
D:\Exercise\CookExample\Chap1\beatles\johnpaul>dir /w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles\johnpaul的目录
[.] [..] john.cpp john.hpp johnpaul.cpp
johnpaul.hpp paul.cpp paul.hpp
6 个文件 819字节
2 个目录 117,802,258,432可用字节
D:\Exercise\CookExample\Chap1\beatles\johnpaul>g++ -c -o john.o john.cpp
D:\Exercise\CookExample\Chap1\beatles\johnpaul>g++ -c -o paul.o paul.cpp
D:\Exercise\CookExample\Chap1\beatles\johnpaul>g++ -c -o johnpaul.o johnpaul.cp
D:\Exercise\CookExample\Chap1\beatles\johnpaul>ar ru libjohnpaul.a john.o paul.
johnpaul.o
ar: creating libjohnpaul.a
D:\Exercise\CookExample\Chap1\beatles\johnpaul>ranlib libjohnpaul.a
D:\Exercise\CookExample\Chap1\beatles\johnpaul>cd..
D:\Exercise\CookExample\Chap1\beatles>cd georgeringo
D:\Exercise\CookExample\Chap1\beatles\georgeringo>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles\georgeringo的目录
[.] [..] george.cpp george.hpp
georgeringo.cpp georgeringo.hpp ringo.cpp ringo.hpp
6 个文件 1,356字节
2 个目录 117,802,246,144可用字节
D:\Exercise\CookExample\Chap1\beatles\georgeringo>g++ -c -o george.o george.cpp
D:\Exercise\CookExample\Chap1\beatles\georgeringo>g++ -c -o ringo.o ringo.cpp
D:\Exercise\CookExample\Chap1\beatles\georgeringo>g++ -c -o georgeringo.o georg
ringo.cpp
D:\Exercise\CookExample\Chap1\beatles\georgeringo>g++ -shared -fPIC -o libgeorg
ringo.so george.o ringo.o georgeringo.o
D:\Exercise\CookExample\Chap1\beatles\georgeringo>g++ -shared -o libgeorgeringo
dll -Wl,--out-implib,libgeorgeringo.a -Wl,--export-all-symbols -Wl,--enable-aut
-image-base george.o ringo.o georgeringo.o
Creating library file: libgeorgeringo.a
D:\Exercise\CookExample\Chap1\beatles\georgeringo>cd..
D:\Exercise\CookExample\Chap1\beatles>cd hellobeatles
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles\hellobeatles的目录
[.] [..] hellobeatles.cpp
1 个文件 213字节
2 个目录 117,794,668,544可用字节
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>g++ -c -o hellobeatles.o hel
obeatles.cpp
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>g++ -o hellobeatles hellobea
les.o ../johnpaul/libjohnpaul.a ../georgeringo/libgeorgeringo.a
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>hellobeatles
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>cd..
D:\Exercise\CookExample\Chap1\beatles>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles的目录
[.] [..] [binaries] [georgeringo] [hellobeatles]
[johnpaul]
0 个文件 0字节
6 个目录 117,790,896,128可用字节
D:\Exercise\CookExample\Chap1\beatles>cd georgeringo
D:\Exercise\CookExample\Chap1\beatles\georgeringo>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles\georgeringo的目录
[.] [..] george.cpp
george.hpp george.o georgeringo.cpp
georgeringo.hpp georgeringo.o libgeorgeringo.a
libgeorgeringo.dll libgeorgeringo.so ringo.cpp
ringo.hpp ringo.o
12 个文件 7,568,495字节
2 个目录 117,790,896,128可用字节
D:\Exercise\CookExample\Chap1\beatles\georgeringo>cd..
D:\Exercise\CookExample\Chap1\beatles>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles的目录
[.] [..] [binaries] [georgeringo] [hellobeatles]
[johnpaul]
0 个文件 0字节
6 个目录 117,790,896,128可用字节
D:\Exercise\CookExample\Chap1\beatles>cd johnpaul
D:\Exercise\CookExample\Chap1\beatles\johnpaul>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles\johnpaul的目录
[.] [..] john.cpp john.hpp john.o
johnpaul.cpp johnpaul.hpp johnpaul.o paul.cpp paul.hpp
paul.o
9 个文件 4,293字节
2 个目录 117,790,896,128可用字节
D:\Exercise\CookExample\Chap1\beatles\johnpaul>ar ru libjohnpaul.a john.o paul.
johnpaul.o
ar: creating libjohnpaul.a
D:\Exercise\CookExample\Chap1\beatles\johnpaul>cd..
D:\Exercise\CookExample\Chap1\beatles>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles的目录
[.] [..] [binaries] [georgeringo] [hellobeatles]
[johnpaul]
0 个文件 0字节
6 个目录 117,798,457,344可用字节
D:\Exercise\CookExample\Chap1\beatles>cd georgeringo
D:\Exercise\CookExample\Chap1\beatles\georgeringo>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles\georgeringo的目录
[.] [..] george.cpp george.hpp
george.o georgeringo.cpp georgeringo.hpp georgeringo.o
ringo.cpp ringo.hpp ringo.o
9 个文件 4,851字节
2 个目录 117,798,457,344可用字节
D:\Exercise\CookExample\Chap1\beatles\georgeringo>g++ -shared -fPIC -o libgeorg
ringo.so george.o ringo.o georgeringo.o
D:\Exercise\CookExample\Chap1\beatles\georgeringo>g++ -shared -o libgeorgeringo
dll -Wl,--out-implib,libgeorgeringo.a -Wl,--export-all-symbols -Wl,--enable-aut
-image-base george.o ringo.o georgeringo.o
Creating library file: libgeorgeringo.a
D:\Exercise\CookExample\Chap1\beatles\georgeringo>cd..
D:\Exercise\CookExample\Chap1\beatles>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles的目录
[.] [..] [binaries] [georgeringo] [hellobeatles]
[johnpaul]
0 个文件 0字节
6 个目录 117,794,660,352可用字节
D:\Exercise\CookExample\Chap1\beatles>cd hellobeatles
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>dir/w
驱动器 D中的卷没有标签。
卷的序列号是 73B6-6CE1
D:\Exercise\CookExample\Chap1\beatles\hellobeatles的目录
[.] [..] hellobeatles.cpp hellobeatles.o
2 个文件 899字节
2 个目录 117,794,660,352可用字节
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>g++ -o hellobeatles hellobea
les.o ../johnpaul/libjohnpaul.a ../georgeringo/libgeorgeringo.a
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>hellobeatles
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>hellobeatles
John, Paul, George, and Ringo
D:\Exercise\CookExample\Chap1\beatles\hellobeatles>
7. 补充:以mytime0为例(代码来自cpp primer plus 5th edition)
// mytime0.cpp -- implementing Time methods#include <iostream>#include "mytime0.h"Time::Time(){ hours = minutes = 0;}Time::Time(int h, int m ){ hours = h; minutes = m;}void Time::AddMin(int m){ minutes += m; hours += minutes / 60; minutes %= 60;}void Time::AddHr(int h){ hours += h;}void Time::Reset(int h, int m){ hours = h; minutes = m;}Time Time::Sum(const Time & t) const{ Time sum; sum.minutes = minutes + t.minutes; sum.hours = hours + t.hours + sum.minutes / 60; sum.minutes %= 60; return sum;}void Time::Show() const{ std::cout << hours << " hours, " << minutes << " minutes";}
// mytime0.h -- Time class before operator overloading#ifndef MYTIME0_H_#define MYTIME0_H_class Time{private: int hours; int minutes;public: Time(); Time(int h, int m = 0); void AddMin(int m); void AddHr(int h); void Reset(int h = 0, int m = 0); Time Sum(const Time & t) const; void Show() const;};#endif
// usetime0.cpp -- using the first draft of the Time class// compile usetime0.cpp and mytime0.cpp together#include <iostream>#include "mytime0.h"int main(){ using std::cout; using std::endl; Time planning; Time coding(2, 40); Time fixing(5, 55);Time total; cout << "planning time = "; planning.Show(); cout << endl; cout << "coding time = "; coding.Show(); cout << endl; cout << "fixing time = "; fixing.Show(); cout << endl; total = coding.Sum(fixing); cout << "coding.Sum(fixing) = "; total.Show(); cout << endl; return 0;}
3.进入CMD并切换到指定文件夹
4.将cpp编译成object文件,头文件会自动被调用,不用管他
编译之后会生成两个.o文件
5.连接object文件生成exe。执行time命令就可以看到系统当前时间
整个编译连接的过程非常繁琐,需要细致的键入命令和文件名
6.直接生成exe,只要这样一条命令也是可以的
- mingw命令行编译示例
- 用MinGW命令行编译链接c/c++
- 如何用MinGW 命令行编译链接c/c++
- 让用MinGW编译的程序使用Unicode命令行参数
- 如何用MinGW 命令行编译链接c/c++
- window7使用MinGW在命令行编译C/C++源程序
- Windows下使用MinGW在命令行编译运行C++程序
- [转载]编译wxWidgets —— windows、vc71、bcc32、MinGW与命令行
- windows下在命令行模式下使用MinGW编译C代码,出现cannot find ipthread
- Linux命令行下编译Android NDK的示例代码
- Linux命令行下编译Android NDK的示例代码
- mingw-w64 i686-7.1.0编译OpenGL超级宝典第五版示例程序
- mingw编译pwlib
- 编译MinGW版GCC
- MinGW 编译 GDIPlus 程序
- 使用MinGW编译Lua
- 使用mingw编译wxPropertyGrid
- windows+mingw+lua 编译
- Apache Options Indexes FollowSymLinks详解
- Android的intent之间复杂参数的传递
- 超酷的杂志翻页效果HTML5实现
- What is a Program?
- 包含tab控件时,,如何让子控件跟随对话框大小变化(下)
- mingw命令行编译示例
- 家乡的小河
- HDU 4293 Groups(12年成都网络赛-F题-DP)
- 编程技能和做员工的技能——哪个更重要?
- Unicode编码相关的一个标准----UTF8
- hibernate lazy(延迟加载) 的使用
- paip.SOCKET抓包工具总结V2012.9.17
- 奢望
- getOutputStream() has already been called for this response] with root cause