拆分代码? [英] Splitting up code ?

查看:69
本文介绍了拆分代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想将包含所有代码的main.c拆分为以下内容


步骤1. Machine.h

步骤2. drand.h和drand.c

步骤3. nrand.h和nrand.c


Main。 c然后只包含一些测试代码。


麻烦从第2步开始。


1. Machine.h应该只包含整数类型。


我已将任何冗余/不需要的注释删除到安全空间。


***开始Machine.h ***


#ifndef __MACHINE_H__

#define __MACHINE_H__


typedef签名__int8 sint8;

typedef unsigned __int8 uint8;

typedef signed __int16 sint16;

typedef unsigned __int16 uint16;

typedef signed __int32 sint32;

typedef unsigned __int32 uint32;

typedef signed __int64 sint64 ;

typedef unsigned __int64 uint64;


#endif


*** Machine.h **结束*


到目前为止,这是有效的。


2.将代码拆分为drand.c和drand.h

这是麻烦的开始;)


***开始drand.h ***


#ifndef __DRAND_H__

#define __DRAND_H__


#include" Machine.h"


struct drand48_data

{

uint16 __x [3];

uint16 __old_x [3];

uint16 __c;

uint16 __init;

uint64 __ a;

};


/ *非重入函数的全局状态。 * /

struct drand48_data __libc_drand48_data;


sint32 __drand48_iterate(uint16 xsubi [3],struct drand48_data * buffer);


#endif


*** drand.h的结尾***


*** drand.c的开头** *


#include" machine.h"

#include" drand.h"


sint32 __drand48_iterate(uint16 xsubi [3],struct drand48_data * buffer)

{

uint64 X;

uint64 result;


if(buffer-> __ init == 0)

{

buffer-> __ a = 0x5deece66du;

buffer-> __ c = 0xb;

buffer-> __ init = 1;

}


X =(uint64) xsubi [2]<< 32 | (uint32)xsubi [1]<< 16 | xsubi [0];


result = X * buffer-> __ a + buffer-> __ c;


xsubi [0] = (uint16)(结果& 0xffff);

xsubi [1] =(uint16)((结果>> 16)& 0xffff);

xsubi [ 2] =(uint16)((结果>>>& 0xffff);


返回0;

}


*** drand.c的结尾***


***开始main.c ***


#include< stdlib.h>

#include< stdio.h>


#include" machine.h"

#include" drand.h"


// nrand interface


//转发声明,以便以后可以分成.h和.c

sint32 nrand48(uint16 xsubi [3]);


// nrand实现


sint32 __nrand48_r(uint16 xsubi [3],struct drand48_data * buffer,sint32

* result)

{


if(__drand48_iterate(xsubi,buffer)< 0)

{

ret urn -1;

}


if(sizeof(unsigned short int)== 2)

{

* result = xsubi [2]<< 15 | xsubi [1]>> 1;

}

其他

{

*结果= xsubi [2]>> 1;

}


返回0;

}


sint32 nrand48( uint16 xsubi [3])

{

sint32 result;


(void)__nrand48_r(xsubi,& __ libc_drand48_data,& ;结果);


返回结果;

}


//测试程序


int main()

{

uint16 state48 [3];


printf("% d \ n",nrand48(state48));

printf("%d \ n",nrand48(state48));

printf("% d \ n",nrand48(state48));

printf("%d \ n",nrand48(state48));

printf("% d \ n",nrand48(state48));


//输出为:


// 1288336289

// 893806678

// 810173679

// 37620279

// 474250687


返回0;

}


*** main.c的结尾***


我是还使用#ifndef和#defi ne和#endif标题的东西是

在大学里学到的......这样做是明智的吗?


因为我看过其他代码*没有*#

标题中的ifndef #define #endif。 ?


(ldpc源代码不在标题中使用它)


我认为视觉c设置是正确的:


1. Machine.h,drand.h包含在标题部分。

2. drand.c和main.c包含在源文件部分。 />

由于某种原因,测试程序main.c不会编译:


------------- -------配置:TestNRand48 - Win32

调试--------------------

编译...

Main.cpp

链接...

Main.obj:错误LNK2001:未解析的外部符号__ int32 __cdecl

__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"

(?__ drand48_iterate @@ YA_HQA_GPAUdrand48_data @@@ Z)

Debug / TestNRand48。 exe:致命错误LNK1120:1未解析的外部

执行link.exe时出错。


TestNRand48.exe - 2个错误,0个警告


为什么会这样?


再见,
Skybuck。

Hi,

I would like to split main.c which contains all code into the following

Step 1. Machine.h
Step 2. drand.h and drand.c
Step 3. nrand.h and nrand.c

Main.c then only contains some test code.

The trouble starts at Step 2.

1. Machine.h should only contain integer types.

I have cut away any redundant/unneeded comments to safe space.

*** begin of Machine.h ***

#ifndef __MACHINE_H__
#define __MACHINE_H__

typedef signed __int8 sint8;
typedef unsigned __int8 uint8;
typedef signed __int16 sint16;
typedef unsigned __int16 uint16;
typedef signed __int32 sint32;
typedef unsigned __int32 uint32;
typedef signed __int64 sint64;
typedef unsigned __int64 uint64;

#endif

*** end of Machine.h ***

So far this worked.

2. Splitting up code into drand.c and drand.h

This is were the trouble starts ;)

*** begin of drand.h ***

#ifndef __DRAND_H__
#define __DRAND_H__

#include "Machine.h"

struct drand48_data
{
uint16 __x[3];
uint16 __old_x[3];
uint16 __c;
uint16 __init;
uint64 __a;
};

/* Global state for non-reentrant functions. */
struct drand48_data __libc_drand48_data;

sint32 __drand48_iterate( uint16 xsubi[3], struct drand48_data *buffer );

#endif

*** end of drand.h ***

*** begin of drand.c ***

#include "machine.h"
#include "drand.h"

sint32 __drand48_iterate( uint16 xsubi[3], struct drand48_data *buffer )
{
uint64 X;
uint64 result;

if (buffer->__init == 0)
{
buffer->__a = 0x5deece66du;
buffer->__c = 0xb;
buffer->__init = 1;
}

X = (uint64) xsubi[2] << 32 | (uint32) xsubi[1] << 16 | xsubi[0];

result = X * buffer->__a + buffer->__c;

xsubi[0] = (uint16) ( result & 0xffff );
xsubi[1] = (uint16) ( (result >> 16) & 0xffff );
xsubi[2] = (uint16) ( (result >> 32) & 0xffff );

return 0;
}

*** end of drand.c ***

*** begin of main.c ***

#include <stdlib.h>
#include <stdio.h>

#include "machine.h"
#include "drand.h"

// nrand interface

// forward declaration so that it can later be split up into .h and .c
sint32 nrand48 (uint16 xsubi[3]);

// nrand implementation

sint32 __nrand48_r ( uint16 xsubi[3], struct drand48_data *buffer, sint32
*result )
{

if (__drand48_iterate (xsubi, buffer) < 0)
{
return -1;
}

if (sizeof (unsigned short int) == 2)
{
*result = xsubi[2] << 15 | xsubi[1] >> 1;
}
else
{
*result = xsubi[2] >> 1;
}

return 0;
}

sint32 nrand48 (uint16 xsubi[3])
{
sint32 result;

(void) __nrand48_r (xsubi, &__libc_drand48_data, &result);

return result;
}

// test program

int main()
{
uint16 state48[3];

printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );
printf("%d \n", nrand48(state48) );

// the output is:

// 1288336289
// 893806678
// 810173679
// 37620279
// 474250687

return 0;
}

*** end of main.c ***

I am also using the #ifndef and #define and #endif stuff for headers as
learned in college... is that wise to do ?

Since I have seen other code *without* #ifndef #define #endif in the
headers. ?

( The ldpc source code does not use it in the headers )

I think the visual c settings are correct:

1. Machine.h, drand.h are included in header section.
2. drand.c and main.c are included in source files section.

For some reason the test program main.c won''t compile:

--------------------Configuration: TestNRand48 - Win32
Debug--------------------
Compiling...
Main.cpp
Linking...
Main.obj : error LNK2001: unresolved external symbol "__int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
(?__drand48_iterate@@YA_HQA_GPAUdrand48_data@@@Z)
Debug/TestNRand48.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

TestNRand48.exe - 2 error(s), 0 warning(s)

Why is that ?

Bye,
Skybuck.

推荐答案

Skybuck Flying< no **** @ hotmail.com>这样说:

Skybuck Flying <no****@hotmail.com> spoke thus:

由于某种原因,测试程序main.c不会编译:


main.c编译正好。您的错误发生在链接

阶段。

我认为视觉设置是正确的:


显然不是,因为链接器不满意。

Main.obj:错误LNK2001:未解析的外部符号" __ int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
( ?__ drand48_iterate @@ YA_HQA_GPAUdrand48_data @@@ Z)
Debug / TestNRand48.exe:致命错误LNK1120:1未解析的外部
执行link.exe时出错。
为什么会这样?
For some reason the test program main.c won''t compile:
main.c compiles just fine. Your error is occurring at the linking
stage.
I think the visual c settings are correct:
Apparently not, since the linker is not happy.
Main.obj : error LNK2001: unresolved external symbol "__int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
(?__drand48_iterate@@YA_HQA_GPAUdrand48_data@@@Z)
Debug/TestNRand48.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe. Why is that ?




这不是最好问的地方:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~ scs / C-faq / top.html
http://benpfaff.org/writings/clc/off-topic.html

尝试与您的平台相关的新闻组。


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



This isn''t the best place to ask:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

Try a newsgroup related to your platform.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


2004年7月7日星期三,Skybuck Flying写道:
On Wed, 7 Jul 2004, Skybuck Flying wrote:

我想将包含所有代码的main.c拆分为以下步骤1. Machine.h
步骤2. drand.h和drand.c
步骤3 .nrand.h和nrand.c

Main.c然后只包含一些测试代码。

麻烦从第2步开始。

1 Machine.h应该只包含整数类型。

我已经删除了任何冗余/不需要的注释到安全空间。


[一串代码剪断]

我也在#ifndef和#define以及#endif的东西用于大学学习......明智的做法是什么?


只有了解了对环境的影响。我喜欢

避免使用标题保护。例如,如果我#include" drand.h"两次,我

编译第一个源文件,它定义了全局

变量的存储空间。然后我编译第二个源文件,它定义了全局变量的存储空间。我试图链接两个目标文件的那一刻我发生了冲突,因为他们都认为他们需要定义全局的

变量。


相反的情况是,标题保护有时会导致源文件无法识别不同文件中的函数或全局变量。

例如,编译器编译第一个源文件,其中包含带有警卫的

头文件。头文件中的#define转换为

下一个源文件被编译,第二个源文件是盲目的

到头文件。

因为我在
标题中看到了其他代码*而没有* #ifndef #define #endif。 ?


以下是我的方法:


- 从单个源文件开始编译并链接好。

- 将我认为的所有函数和全局变量与/ b $ b drand.c复制到drand.c中。

- 创建一个包含原型的drand.h所有

函数和全局变量的extern声明。

- #include" drand.h"在drand.c。如果您更改了drand.c中的信息而没有更改drand.h中的信息,这将生成编译时错误



- 创建一个main.c(但称之为更具描述性的#include

" drand.h"并且刚刚测试了drand.c中函数的代码。


如果你想要你也可以将一些typedef和macro行移动到

machine.h 。最重要的规则是永远不要在

标头中创建定义。这意味着标题永远不会分配存储空间,因此可以多次使用
#included。这消除了对标题保护的需要。

(ldpc源代码不在标题中使用它)

我认为视觉c设置是正确的:

1. Machine.h,drand.h包含在标题部分。
2. drand.c和main.c包含在源文件部分。

由于某种原因测试程序main.c不会编译:

--------------------配置:TestNRand48 - Win32
调试--------------------
编译......
Main.cpp
链接......
Main.obj :错误LNK2001:未解析的外部符号" __ int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
(?__ drand48_iterate @@ YA_HQA_GPAUdrand48_data @@@ Z)
Debug / TestNRand48 .exe:致命错误LNK1120:1未解析的外部
执行link.exe时出错。

TestNRand48.exe - 2个错误,0个警告
为什么会这样?
Hi,

I would like to split main.c which contains all code into the following

Step 1. Machine.h
Step 2. drand.h and drand.c
Step 3. nrand.h and nrand.c

Main.c then only contains some test code.

The trouble starts at Step 2.

1. Machine.h should only contain integer types.

I have cut away any redundant/unneeded comments to safe space.
[bunch of code snipped]
I am also using the #ifndef and #define and #endif stuff for headers as
learned in college... is that wise to do ?
Only if you understand the implications for your environment. I like to
avoid using header guards. For example, if I #include "drand.h" twice, I
compile the first source file and it defines storage for the global
variables. I then compile the second source file and it defines storage for
the global variables. The moment I attempt to link the two object files I
get a conflict because they both thought they needed to define the global
variable.

The opposite situation is that header guards can sometimes cause a source
file to not be aware of a function or global variable in a different file.
For example, the compiler compiles the first source file that includes a
header file with guards. The #define from the header file carries over to
the next source file getting compiled and the second source file is blind
to the header file.
Since I have seen other code *without* #ifndef #define #endif in the
headers. ?
Here is how I would do it:

- Start with a single source file that compiles and links okay.
- Copy all the functions and global varaibles that I believe goes with
drand.c into drand.c.
- Create a drand.h that has prototypes and extern declarations for all the
functions and global variables.
- #include "drand.h" in drand.c. This will generate a compile time error
if you ever change the information in drand.c but not in drand.h
- create a main.c (but call it something more descriptive) that #includes
"drand.h" and has just test code for the functions in drand.c

If you want you can also move some of the typedef and macro lines into
machine.h. The most important rule is never create a definition in a
header. This means headers never allocate storage and can therefore be
#included multiple times. This eliminates the need for header guards.
( The ldpc source code does not use it in the headers )

I think the visual c settings are correct:

1. Machine.h, drand.h are included in header section.
2. drand.c and main.c are included in source files section.

For some reason the test program main.c won''t compile:

--------------------Configuration: TestNRand48 - Win32
Debug--------------------
Compiling...
Main.cpp
Linking...
Main.obj : error LNK2001: unresolved external symbol "__int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
(?__drand48_iterate@@YA_HQA_GPAUdrand48_data@@@Z)
Debug/TestNRand48.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

TestNRand48.exe - 2 error(s), 0 warning(s)

Why is that ?




这看起来就像你只编译了main.cp p并忘了在你的项目中添加drand.c




-

发送电子邮件至:darrell at cs dot toronto dot edu

不要发送电子邮件至 vi * ***********@whitehouse.gov




" Darrell Grainger" <哒***** @ NOMORESPAMcs.utoronto.ca.com>在消息中写道

新闻:Pi ******************************* @ drj.pf。 ..

"Darrell Grainger" <da*****@NOMORESPAMcs.utoronto.ca.com> wrote in message
news:Pi*******************************@drj.pf...
2004年7月7日星期三,Skybuck Flying写道:
On Wed, 7 Jul 2004, Skybuck Flying wrote:


我想拆分主要。 c其中包含以下所有代码

步骤1. Machine.h
步骤2. drand.h和drand.c
步骤3.nrand.h和nrand.c

Main.c然后只包含一些测试代码。

麻烦从第2步开始。

1. Machine.h应该只包含整数类型。

我已经删除了任何冗余/不需要的注释到安全空间。
[一堆代码剪断]
Hi,

I would like to split main.c which contains all code into the following

Step 1. Machine.h
Step 2. drand.h and drand.c
Step 3. nrand.h and nrand.c

Main.c then only contains some test code.

The trouble starts at Step 2.

1. Machine.h should only contain integer types.

I have cut away any redundant/unneeded comments to safe space.
[bunch of code snipped]
我也在使用在大学里学到的#ifndef和#define以及#endif标题的内容是明智的吗?
I am also using the #ifndef and #define and #endif stuff for headers as
learned in college... is that wise to do ?



只有了解了对环境的影响。我喜欢
避免使用标题守卫。例如,如果我#include" drand.h"两次,我编译第一个源文件,它定义了全局变量的存储空间。然后我编译第二个源文件,它为全局变量定义存储



Only if you understand the implications for your environment. I like to
avoid using header guards. For example, if I #include "drand.h" twice, I
compile the first source file and it defines storage for the global
variables. I then compile the second source file and it defines storage



。我试图链接两个目标文件的那一刻我发生了冲突,因为他们都认为他们需要定义全局变量。

相反的情况是标题保护可以有时会导致源文件无法识别不同文件中的函数或全局变量。
例如,编译器编译包含带有警卫的
头文件的第一个源文件。头文件中的#define继续编译下一个源文件,第二个源文件对头文件是盲目的。


for the global variables. The moment I attempt to link the two object files I
get a conflict because they both thought they needed to define the global
variable.

The opposite situation is that header guards can sometimes cause a source
file to not be aware of a function or global variable in a different file.
For example, the compiler compiles the first source file that includes a
header file with guards. The #define from the header file carries over to
the next source file getting compiled and the second source file is blind
to the header file.

从我开始在
标题中看到了其他代码*而没有* #ifndef #define #endif。 ?
我将如何做到这一点:

- 从一个编译和链接的单个源文件开始。
- 复制我相信的所有功能和全局变量将
drand.c放入drand.c。
- 创建一个drand.h,它包含所有
函数和全局变量的原型和extern声明。
- #include" drand.h"在drand.c。这将产生编译时错误
如果您更改drand.c中的信息但不在drand.h中。
- 创建一个main.c(但称之为更具描述性的)#includes
" drand.h"并且刚刚测试了drand.c中函数的代码。
Since I have seen other code *without* #ifndef #define #endif in the
headers. ?
Here is how I would do it:

- Start with a single source file that compiles and links okay.
- Copy all the functions and global varaibles that I believe goes with
drand.c into drand.c.
- Create a drand.h that has prototypes and extern declarations for all the
functions and global variables.
- #include "drand.h" in drand.c. This will generate a compile time error
if you ever change the information in drand.c but not in drand.h
- create a main.c (but call it something more descriptive) that #includes
"drand.h" and has just test code for the functions in drand.c




谢谢我稍后会试一试并告诉你它是怎么回事;):D ...看看我的

下面的其他问题;)

如果你想要你也可以将一些typedef和macro行移动到
machine.h中。最重要的规则是永远不要在
标题中创建定义。这意味着标题永远不会分配存储空间,因此可以多次#include #included。这消除了对头部防护装置的需要。


好​​的,这么快的问题:


难道不会导致多次包括前瞻声明吗?

所以换句话说我的问题是:


可以多次使用相同的前向声明吗?


我想不是吗?


例如:


int nrand48(short int blabla [3]);


int nrand48(short int blabla [3]);


int nrand48(short int blabla [3]);


int nrand48(short int blabla [3]);


这是4个前向声明...


这是允许的吗? ?



Thanks I will try this out later and tell you how it goes ;) :D... see my
other question below ;)

If you want you can also move some of the typedef and macro lines into
machine.h. The most important rule is never create a definition in a
header. This means headers never allocate storage and can therefore be
#included multiple times. This eliminates the need for header guards.
Ok, so quick question:

Won''t that lead to including forward declarations many times ?

So in other words my question would be:

Is it ok to have the same forward declaration multiple times ?

I think not ?

for example:

int nrand48( short int blabla[3] );

int nrand48( short int blabla[3] );

int nrand48( short int blabla[3] );

int nrand48( short int blabla[3] );

Those are 4 forward declarations...

Is that allowed in C ???

(ldpc源代码不在标题中使用它)

我认为视觉c设置是正确的:
1. Machine.h,drand.h包含在标题部分。
2. drand.c和main.c包含在源文件部分。

由于某种原因测试程序main.c不会编译:

--------------------配置:TestNRand48 - Win32
调试---------------- ----
编译......
Main.cpp
链接......
Main.obj:错误LNK2001:未解析的外部符号__ int32 __cdecl
__drand48_iterate (unsigned __int16 * const,struct drand48_data *)"
(?__ drand48_iterate @@ YA_HQA_GPAUdrand48_data @@@ Z)
Debug / TestNRand48.exe:致命错误LNK1120:1未解析的外部
执行错误link.exe。

TestNRand48.exe - 2个错误,0个警告

为什么会这样?
( The ldpc source code does not use it in the headers )

I think the visual c settings are correct:

1. Machine.h, drand.h are included in header section.
2. drand.c and main.c are included in source files section.

For some reason the test program main.c won''t compile:

--------------------Configuration: TestNRand48 - Win32
Debug--------------------
Compiling...
Main.cpp
Linking...
Main.obj : error LNK2001: unresolved external symbol "__int32 __cdecl
__drand48_iterate(unsigned __int16 * const,struct drand48_data *)"
(?__drand48_iterate@@YA_HQA_GPAUdrand48_data@@@Z)
Debug/TestNRand48.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

TestNRand48.exe - 2 error(s), 0 warning(s)

Why is that ?


这看起来就像你只编译了main.cpp并忘了将drand.c
添加到你的项目中。

-
发送电子邮件至:darrell at cs dot toronto dot edu
请勿发送电子邮件至 vi ******* *****@whitehouse.gov



This just looks like you compiled only main.cpp and forgot to add drand.c
to your project.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don''t send e-mail to vi************@whitehouse.gov



这篇关于拆分代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆