【问题标题】:c project compile error [closed]c项目编译错误[关闭]
【发布时间】:2015-12-21 07:27:59
【问题描述】:

好的,我所有的文件都是这样的

sendmail.h 在这个文件中声明了一些函数和一些常量

#define STMP_SERVER "smtp.exmail.qq.com"
#define LOGIN_NAME "secbot@test.com"
#define LOGIN_PASSOWRD "123456"
#define MAIL_TO "root@test.com"


char* get_local_addr(char* buf, size_t len);

int base64_encode(unsigned char *buf, int nLen, char *pOutBuf, int nBufSize);

void sendemail(char *smtpServer, char *serveruser, char *serverpassword, char *serverip);

int open_socket(struct sockaddr *addr);

sendmail.c 有一些类似于 sendmail.h 的功能

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stddef.h>

..............

test.c 是主函数

#include "sendmail.h"


#define SERVERNAME "root"
#define SERVERPASSWORD "123456"


int main()
{   
    sendemail(STMP_SERVER, SERVERNAME, SERVERPASSWORD,NULL);
    return 0;
}

但是编译错误然后说这个

[root@ops test]# gcc -o test test.c 
In file included from test.c:1:
sendmail.h:10: error: expected declaration specifiers or ‘...’ before ‘size_t’
sendmail.h:16: warning: ‘struct sockaddr’ declared inside parameter list
sendmail.h:16: warning: its scope is only this definition or declaration, which is probably not what you want
test.c: In function ‘main’:
test.c:10: error: ‘NULL’ undeclared (first use in this function)
test.c:10: error: (Each undeclared identifier is reported only once
test.c:10: error: for each function it appears in.)

【问题讨论】:

  • a) 使用正确的大小写 b) 包括 importantrelevant 代码 c) 这意味着您多次声明了相同的函数(相同的名称) .
  • 一般来说,你不应该让预处理器包含*.c文件。

标签: c compiler-errors


【解决方案1】:
  1. 停止在a.c 中使用#include "sendmail.c" 之类的东西
  2. 创建一个头文件,其中包含sendmail.c 中函数的声明
  3. #include 来自a.c头文件

【讨论】:

  • 1.i 用户 sedmail.h 2.a.c 用户 #include sendmail.h 但像这样 test.c:(.text+0x19): undefined reference to `sendemail'
  • 链接所有需要的文件。
  • 看到问题,我修改它
  • #includes 中的sendmail.c 移动到sendmail.h
  • 是的,我尝试这样做,但他们说 /tmp/ccK66kVq.o: In function main': test.c:(.text+0x19): undefined reference to sendemail' collect2: ld returned 1 exit status
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 2013-03-05
  • 2016-03-20
  • 1970-01-01
相关资源
最近更新 更多