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

来源:互联网 发布:好一点的耳环品牌 知乎 编辑:程序博客网 时间:2024/06/07 12:17

 一、安装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.dllclass AE_DEMO_ENTRY CTest {public:CTest();virtual ~CTest();public:int Add(int a, int b);protected:intm_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 variableAE_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 definitionCTest::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)/binBINDIR= $(FEPHOME)/binLIBDIR= $(FEPHOME)/libCODEDIR= $(FEPHOME)/codeINCPATH= -I../inc -I$(FEPHOME)/code/include####### Compiler, tools and optionsAR= ar CXX= g++CC= cc LD= g++MAKE= makeGM= gmakeYACC= yaccLEX= lexTAR           = tar -cfCOMPRESS     = gzip -9fCOPY         = cp -fSED          = sedCOPY_FILE    = $(COPY)COPY_DIR     = $(COPY) -RSTRIP        = INSTALL_FILE  = $(COPY_FILE)INSTALL_DIR   = $(COPY_DIR)INSTALL_PROGRAM = $(COPY_FILE)DEL_FILE      = rm -fSYMLINK       = ln -f -sDEL_DIR       = rmdirMOVE          = mv -fCHK_DIR_EXISTS= test -dMKDIR         = mkdir -p####### Compiler, flags definitionsARFLAGS  = -vrCFLAGS   = -g CXXFLAGS = -c -fpic -w -gLDCFLAGS = -G -z defs -lsocket -lnsl -ldl LDCXXFLAGS = -shared CLDFLAGS = -gCXXLDFLAGS = -g -GBINLIB = -lcTHREAD_LIB = -lpthreadSOCKET_LIB = -lsocket -lnslDYLOAD_LIB = -ldl

libdemo的Makefile

PRJNAME= libdemoPRJOUT= /FEPDebuginclude $(FEPHOME)/code/include/makefile_linux####### FilesSRCDIR=../srcSOURCES = stdafx.cpp \  demo.cppOBJECTS = $(OBJDIR)/stdafx.o \  $(OBJDIR)/demo.o TARGET= $(DLLDIR)/$(PRJNAME).soTARGETA = $(LIBDIR)/$(PRJNAME).a####### Build rulesall: 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 dependenciesdepend:@(mkdepend   -p$(OBJDIR)/ -o.o $(INCPATH) $(SOURCES))>/dev/null 2>&1@rm -f makefile.bakclean: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= testdemoPRJOUT= /FEPDebuginclude $(FEPHOME)/code/include/makefile_linux####### FilesSRCDIR=../srcSOURCES = stdafx.cpp \  testdemo.cppOBJECTS = $(OBJDIR)/stdafx.o \  $(OBJDIR)/testdemo.o FEPLIB = -L$(DLLDIR) -ldemo OUTBIN = ${BINDIR}/testdemo####### Build rulesall: obj_dir $(OUTBIN)obj_dir:@$(CHK_DIR_EXISTS) $(OBJDIR) || $(MKDIR) $(OBJDIR)$(OUTBIN): $(OBJECTS)  $(CXX) -o $@ $(OBJECTS) $(CXXLIB) $(FEPLIB) $(THREAD_LIB) $(DYLOAD_LIB)####### Include file dependenciesdepend:@(mkdepend   -p$(OBJDIR)/ -o.o $(INCPATH) $(SOURCES))>/dev/null 2>&1@rm -f makefile.bakclean: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.
原创粉丝点击