除了命令行编译之外,我还尝试了一个makefile。
这是命令行编译的结果
neweddy:~/testdir/realworld$ g++ main.cpp lights.cpp
/tmp/ccASBZtK.o:(.bss+0x0): multiple definition of `red'
/tmp/cco6q58J.o:(.bss+0x0): first defined here
/tmp/ccASBZtK.o:(.bss+0x4): multiple definition of `blue'
/tmp/cco6q58J.o:(.bss+0x4): first defined here
/tmp/ccASBZtK.o:(.bss+0x8): multiple definition of `white'
/tmp/cco6q58J.o:(.bss+0x8): first defined here
/tmp/ccASBZtK.o:(.data+0x0): multiple definition of `dutyRed'
/tmp/cco6q58J.o:(.data+0x0): first defined here
/tmp/ccASBZtK.o:(.data+0x4): multiple definition of `dutyBlue'
/tmp/cco6q58J.o:(.data+0x4): first defined here
/tmp/ccASBZtK.o:(.data+0x8): multiple definition of `dutyWhite'
/tmp/cco6q58J.o:(.data+0x8): first defined here
/tmp/ccASBZtK.o: In function `mraa::Pwm::Pwm(int, int, bool)':
lights.cpp:(.text._ZN4mraa3PwmC2Eiib[_ZN4mraa3PwmC5Eiib]+0x1b): undefined reference to `mraa_pwm_init'
lights.cpp:(.text._ZN4mraa3PwmC2Eiib[_ZN4mraa3PwmC5Eiib]+0x34): undefined reference to `mraa_pwm_init_raw'
lights.cpp:(.text._ZN4mraa3PwmC2Eiib[_ZN4mraa3PwmC5Eiib]+0xd1): undefined reference to `mraa_pwm_owner'
/tmp/ccASBZtK.o: In function `mraa::Pwm::~Pwm()':
lights.cpp:(.text._ZN4mraa3PwmD2Ev[_ZN4mraa3PwmD5Ev]+0xf): undefined reference to `mraa_pwm_close'
/tmp/ccASBZtK.o: In function `mraa::Pwm::enable(bool)':
lights.cpp:(.text._ZN4mraa3Pwm6enableEb[_ZN4mraa3Pwm6enableEb]+0x23): undefined reference to `mraa_pwm_enable'
lights.cpp:(.text._ZN4mraa3Pwm6enableEb[_ZN4mraa3Pwm6enableEb]+0x3a): undefined reference to `mraa_pwm_enable'
/tmp/ccASBZtK.o: In function `mraa::Pwm::config_ms(int, float)':
lights.cpp:(.text._ZN4mraa3Pwm9config_msEif[_ZN4mraa3Pwm9config_msEif]+0x1d): undefined reference to `mraa_pwm_config_ms'
collect2: error: ld returned 1 exit status
这是makefile编译的结果
neweddy:~/testdir/realworld$ make
g++ -Wall -c main.cpp
g++ -Wall -c lights.cpp
g++ -Wall -o realworld main.o lights.o
lights.o:(.bss+0x0): multiple definition of `red'
main.o:(.bss+0x0): first defined here
lights.o:(.bss+0x4): multiple definition of `blue'
main.o:(.bss+0x4): first defined here
lights.o:(.bss+0x8): multiple definition of `white'
main.o:(.bss+0x8): first defined here
lights.o:(.data+0x0): multiple definition of `dutyRed'
main.o:(.data+0x0): first defined here
lights.o:(.data+0x4): multiple definition of `dutyBlue'
main.o:(.data+0x4): first defined here
lights.o:(.data+0x8): multiple definition of `dutyWhite'
main.o:(.data+0x8): first defined here
lights.o: In function `mraa::Pwm::Pwm(int, int, bool)':
lights.cpp:(.text._ZN4mraa3PwmC2Eiib[_ZN4mraa3PwmC5Eiib]+0x1b): undefined reference to `mraa_pwm_init'
lights.cpp:(.text._ZN4mraa3PwmC2Eiib[_ZN4mraa3PwmC5Eiib]+0x34): undefined reference to `mraa_pwm_init_raw'
lights.cpp:(.text._ZN4mraa3PwmC2Eiib[_ZN4mraa3PwmC5Eiib]+0xd1): undefined reference to `mraa_pwm_owner'
lights.o: In function `mraa::Pwm::~Pwm()':
lights.cpp:(.text._ZN4mraa3PwmD2Ev[_ZN4mraa3PwmD5Ev]+0xf): undefined reference to `mraa_pwm_close'
lights.o: In function `mraa::Pwm::enable(bool)':
lights.cpp:(.text._ZN4mraa3Pwm6enableEb[_ZN4mraa3Pwm6enableEb]+0x23): undefined reference to `mraa_pwm_enable'
lights.cpp:(.text._ZN4mraa3Pwm6enableEb[_ZN4mraa3Pwm6enableEb]+0x3a): undefined reference to `mraa_pwm_enable'
lights.o: In function `mraa::Pwm::config_ms(int, float)':
lights.cpp:(.text._ZN4mraa3Pwm9config_msEif[_ZN4mraa3Pwm9config_msEif]+0x1d): undefined reference to `mraa_pwm_config_ms'
collect2: error: ld returned 1 exit status
makefile:2: recipe for target 'realworld' failed
make: *** [realworld] Error 1
这是我的makefile
realworld: main.o lights.o
g++ -Wall -o realworld main.o lights.o
main.o: main.cpp lights.h
g++ -Wall -c main.cpp
lights.o: lights.cpp lights.h
g++ -Wall -c lights.cpp
clean:
rm *.o
任何人都知道我做错了什么?
谢谢!
编辑:这是#include
主文件
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include "lights.h"
int initSocket();
int main(int argc, char** argv){
char recBuff[255];
char sendBuff[255];
bzero(recBuff, sizeof(recBuff));
bzero(sendBuff, sizeof(sendBuff));
initLights();
//int n = 0;
int sock, clientsock;
int running = 1;
sock = initSocket();
while(running){
clientsock = accept(sock, (struct sockaddr*)NULL,NULL);
printf("Got client\n");
read(clientsock,recBuff,255);
//memcpy(&dutyRed, recBuff, sizeof(float));
//printf("red %f\n", dutyRed);
running = 0;
}
closeLights();
}
int initSocket(){
int sockfd, portno;
portno = 31002;
int yes = 1;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET,SOCK_STREAM,0);
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
perror("setsockopt\n");
exit(1);
}
if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
perror("error on bind\n");
exit(1);
}
listen(sockfd,5);
printf("waiting for connection...\n");
return sockfd;
}
灯光.cpp
#include "lights.h"
int initLights(){
red = new mraa::Pwm(3);
if (red == NULL) {
return MRAA_ERROR_UNSPECIFIED;
}
blue = new mraa::Pwm(5);
if (blue == NULL) {
return MRAA_ERROR_UNSPECIFIED;
}
white = new mraa::Pwm(6);
if (white == NULL) {
return MRAA_ERROR_UNSPECIFIED;
}
return 0;
}
void setLights(float dutyRed, float dutyBlue, float dutyWhite){
red->enable(true);
blue->enable(true);
white->enable(true);
red->config_ms(1, dutyRed);
blue->config_ms(1, dutyBlue);
white->config_ms(1, dutyWhite);
}
void closeLights(){
delete red;
delete blue;
delete white;
}
灯.h
#ifndef LIGHTS_H
#define LIGHTS_H
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include "mraa.hpp"
mraa::Pwm* red;
mraa::Pwm* blue;
mraa::Pwm* white;
float dutyRed = 0.5;
float dutyBlue = 0.5;
float dutyWhite = 0.5;
int initLights();
void setLights(float,float,float);
void closeLights();
#endif