您的位置:首页 > 编程语言

在opensuse操作系统下简单编程指南

2011-08-31 11:41 323 查看
一、安装gcc和make

1、正常安装之后opensuse操作系统没有安装gcc和make,需要自己安装。首先从“计算机”开始选择“安装软件”。



2、从软件包管理器选择程序“programming”,从右边栏中找到 gcc和make,点击右下角应用和安装即可。



二、用户环境变量设置

用root用户登录系统,进行如下相应的操作。Windows环境变量比较简单这里就不再描述了。

A、编辑/etc/profile,增加自定义的环境变量:

FEPHOME=/openSuse3000/fep

LD_LIBRARY_PATH=$ FEPHOME/bin:$ LD_LIBRARY_PATH

export FEPHOME LD_LIBRARY_PATH

B、生效命令Source

#Source /etc/profile

C、查看是否生效

#echo $FEPHOME

D、查看环境变量

#env

三、动态链接库范例

可以使用VC++6.0生成一个动态链接库libdemo,libdemo目录下包含inc、src、unix和win32,其中inc目录存放*.h文件,src目录存放*.c/*.cpp文件,unix目录存放MakeFile,win32存放vc++6.0的*.dsp和*.dsw等。

Linux

/ openSuse3000/fep

|_bin

|_lib

|_code

|_include

|_test_sys

libdemo

|_inc

|_src

|_uinx

|_win32

testdemo

|_inc
|_src
|_uinx
|_win32

/FEPDebug

|_libdemo

|_testdemo

libdemo存放stdafx.o和demo.o目标文件;testdemo存放stdafx.o和testdemo.o目标文件。

#stdafx.h
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__E5CE9468_AAF9_46A6_979B_9C125F6E9D00__INCLUDED_)
#define AFX_STDAFX_H__E5CE9468_AAF9_46A6_979B_9C125F6E9D00__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <stdio.h>

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__E5CE9468_AAF9_46A6_979B_9C125F6E9D00__INCLUDED_)

#stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
//	libdemo.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

#demo.h
#ifndef _DEMO_H_
#define _DEMO_H_

// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the AE_DEMO_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// AE_DEMO_ENTRY functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef WIN32
#ifdef AE_DEMO_EXPORTS
#define AE_DEMO_ENTRY __declspec(dllexport)
#else
#define AE_DEMO_ENTRY __declspec(dllimport)
#endif
#else
#define AE_DEMO_ENTRY
#endif

// This class is exported from the libdemo.dll
class AE_DEMO_ENTRY CTest
{
public:
CTest();
virtual ~CTest();
public:
int Add(int a, int b);
protected:
int	m_nCount;
};

extern AE_DEMO_ENTRY int nTest;

AE_DEMO_ENTRY int fnTest();

#endif // _DEMO_H_

#demo.cpp
#include "stdafx.h"
#include "demo.h"

// This is an example of an exported variable
AE_DEMO_ENTRY int nTest = 0x64;

// This is an example of an exported function.
AE_DEMO_ENTRY int fnTest()
{
return 64;
}

// This is the constructor of a class that has been exported.
// see demo.h for the class definition
CTest::CTest()
{
}

CTest::~CTest()
{
}

int CTest::Add(int a, int b)
{
return (a + b);
}

四、测试动态链接库范例

// testdemo.cpp : Defines the entry point for the console application.
//
// 把demo.h拷贝至$(FEPHOME)/include目录中.

#include "stdafx.h"
#include "demo.h"

int main(int argc, char* argv[])
{
CTest test;
int nSum = test.Add(1, 2);
printf("a=1,b=2,sum=%d\n", nSum);

printf("nTest=%d\n", nTest);

printf("int fnTest() return %d\n", fnTest());
return 0;
}

执行结果:

linux-qf6g:/openSusu3000/fep/bin # ./testdemo
a=1,b=2,sum=3
nTest=100
int fnTest() return 64

五、MakeFile编写

makefile_linux

#############################################################################
# Title	: makefile_linux(opensuse 11.0)
# Author	: liuxuezong
# Project	: PRJNAME
# Generated by 2011.08.31 14:56
#############################################################################

OBJDIR		= $(PRJOUT)/$(PRJNAME)
DLLDIR		= $(FEPHOME)/bin
BINDIR		= $(FEPHOME)/bin
LIBDIR		= $(FEPHOME)/lib
CODEDIR		= $(FEPHOME)/code
INCPATH		= -I../inc -I$(FEPHOME)/code/include

####### Compiler, tools and options
AR		= ar
CXX		= g++
CC		= cc
LD		= g++
MAKE		= make
GM		= gmake
YACC		= yacc
LEX		= lex
TAR           	= tar -cf
COMPRESS     	= gzip -9f
COPY         	= cp -f
SED          	= sed
COPY_FILE    	= $(COPY)
COPY_DIR     	= $(COPY) -R
STRIP        	=
INSTALL_FILE  	= $(COPY_FILE)
INSTALL_DIR   	= $(COPY_DIR)
INSTALL_PROGRAM = $(COPY_FILE)
DEL_FILE      	= rm -f
SYMLINK       	= ln -f -s
DEL_DIR       	= rmdir
MOVE          	= mv -f
CHK_DIR_EXISTS	= test -d
MKDIR         	= mkdir -p

####### Compiler, flags definitions
ARFLAGS  	= -vr
CFLAGS   	= -g
CXXFLAGS 	= -c -fpic -w -g
LDCFLAGS 	= -G -z defs -lsocket -lnsl -ldl
LDCXXFLAGS 	= -shared
CLDFLAGS 	= -g
CXXLDFLAGS 	= -g -G
BINLIB 		= -lc
THREAD_LIB 	= -lpthread
SOCKET_LIB 	= -lsocket -lnsl
DYLOAD_LIB 	= -ldl

libdemo的Makefile

PRJNAME	= libdemo
PRJOUT	= /FEPDebug

include $(FEPHOME)/code/include/makefile_linux

####### Files
SRCDIR	=../src

SOURCES = stdafx.cpp \
demo.cpp

OBJECTS = $(OBJDIR)/stdafx.o \
$(OBJDIR)/demo.o

TARGET	= $(DLLDIR)/$(PRJNAME).so
TARGETA = $(LIBDIR)/$(PRJNAME).a

####### Build rules

all: obj_dir $(TARGET) $(TARGETA)

obj_dir:
@$(CHK_DIR_EXISTS) $(OBJDIR) || $(MKDIR) $(OBJDIR)

$(TARGET): $(OBJECTS)
$(LD) $(LDCXXFLAGS) -o $@ $(OBJECTS) $(CXXLIB)

$(TARGETA): $(OBJECTS)
$(AR) $(ARFLAGS) $@ $(OBJECTS)

####### Include file dependencies

depend:
@(mkdepend   -p$(OBJDIR)/ -o.o $(INCPATH) $(SOURCES))>/dev/null 2>&1
@rm -f makefile.bak

clean:
rm -f $(OBJECTS)
rm -f $(TARGET)
rm -f $(TARGETA)

####### Compile
$(OBJDIR)/stdafx.o: $(SRCDIR)/stdafx.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $?

$(OBJDIR)/demo.o: $(SRCDIR)/demo.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $?

# DO NOT DELETE THIS LINE -- make depend depends on it.


testdemo的Makefile
PRJNAME	= testdemo
PRJOUT	= /FEPDebug

include $(FEPHOME)/code/include/makefile_linux

####### Files
SRCDIR	=../src

SOURCES = stdafx.cpp \
testdemo.cpp

OBJECTS = $(OBJDIR)/stdafx.o \
$(OBJDIR)/testdemo.o

FEPLIB = -L$(DLLDIR) -ldemo
OUTBIN = ${BINDIR}/testdemo

####### Build rules

all: obj_dir $(OUTBIN)

obj_dir:
@$(CHK_DIR_EXISTS) $(OBJDIR) || $(MKDIR) $(OBJDIR)

$(OUTBIN): $(OBJECTS)
$(CXX) -o $@ $(OBJECTS) $(CXXLIB) $(FEPLIB) $(THREAD_LIB) $(DYLOAD_LIB)

####### Include file dependencies

depend:
@(mkdepend   -p$(OBJDIR)/ -o.o $(INCPATH) $(SOURCES))>/dev/null 2>&1
@rm -f makefile.bak

clean:
rm -f $(OBJECTS)
rm -f $(OUTBIN)

####### Compile
$(OBJDIR)/stdafx.o: $(SRCDIR)/stdafx.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $?

$(OBJDIR)/testdemo.o: $(SRCDIR)/testdemo.cpp
$(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $?

# DO NOT DELETE THIS LINE -- make depend depends on it.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: