vs2013生成lib

来源:互联网 发布:php class 内调用方法 编辑:程序博客网 时间:2024/04/30 14:46

引擎cocos2d-x-3.1.1

一、 cocos创建一个项目,随便是lua还是cpp。这里用cpp演示

二、创建完成之后运行下项目

之后创建两个类。如下

TestLib.cpp文件

#include "TestLib.h"#include "People.h"Scene* TestLib::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();        // 'layer' is an autorelease objectauto layer = TestLib::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool TestLib::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }        Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();    /////////////////////////////    // 2. add a menu item with "X" image, which is clicked to quit the program    //    you may modify it.    // add a "close" icon to exit the progress. it's an autorelease object    auto closeItem = MenuItemImage::create(                                           "CloseNormal.png",                                           "CloseSelected.png",   CC_CALLBACK_1(TestLib::menuCloseCallback, this));    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,                                origin.y + closeItem->getContentSize().height/2));    // create menu, it's an autorelease object    auto menu = Menu::create(closeItem, NULL);    menu->setPosition(Vec2::ZERO);    this->addChild(menu, 1);    /////////////////////////////    // 3. add your codes below...    // add a label shows "Hello World"    // create and initialize a label    lab_ = LabelTTF::create("Hello World", "Arial", 24);        // position the label on the center of the screenlab_->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height - lab_->getContentSize().height));    // add the label as a child to this layerthis->addChild(lab_, 1);    // add "HelloWorld" splash screen"    auto sprite = Sprite::create("HelloWorld.png");    // position the sprite on the center of the screen    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));    // add the sprite as a child to this layer    this->addChild(sprite, 0);        return true;}void TestLib::menuCloseCallback(Ref* pSender){/*#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");    return;#endif    Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif*/lab_->setString("using test lib");People* sp = People::create();sp->setPosition(ccp(200, 300));this->addChild(sp);}




TestLib.h文件
#ifndef __TestLib_H__#define __TestLib_H__#include "cocos2d.h"USING_NS_CC;class TestLib : public cocos2d::Layer{public:    // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();          // a selector callback    void menuCloseCallback(cocos2d::Ref* pSender);        // implement the "static create()" method manuallyCREATE_FUNC(TestLib);LabelTTF* lab_;};#endif // __HELLOWORLD_SCENE_H__


people.cpp文件

#include "People.h"People::People(){}People::~People(){}bool People::init(){if (!Node::init()){return false;}Sprite* sp = Sprite::create("CloseNormal.png");addChild(sp);return true;}


people.h文件

#ifndef __People_H__#define __People_H__#include "cocos2d.h"USING_NS_CC;class People : public cocos2d::Node{public:People();~People();    virtual bool init();  CREATE_FUNC(People);};#endif // __HELLOWORLD_SCENE_H__



三、 两个试验类已经创建完成,这个时候运行下程序。并且修改AppDelegate,原先是引到helloworld,改成引到testlib。程序运行通过就成,是为了保证试验类没有错误。


四、修改工程配置。

1 打开工程属性




2 找到配置属性-》常规-》配置类型    改成静态库




3  选择完成后点-》应用-》确定。之后就是生成lib



4 生成的文件在  testCpp\proj.win32\Debug.win32该目录下



5 新建另一个cpp工程,这两个加上testlib.h和people.h,一共四个文件复制到新工程的class目录下

使用lib库有两个方法,一个是直接把lib引到工程目录下,在需要用到的地方include一下就可以。代码是:#pragma comment(lib, "testCpp.lib")

另外一个方法是直接设置库路径,就不用把lib引到工程目录下了。直接点开工程属性,在配置属性-》连接器-》常规-》附加库目录中添加库





之后确认。


在配置属性-》连接器-》输入-》附加依赖项。如图填写



之后确定,运行工程就可以了


说明   .obj的文件不需要像lib一样。直接添加到工程目录下直接用就可以


0 0
原创粉丝点击