I wrote a code that accesses LEDs on an FPGA. Anyway, I cannot successfully compile the following code in Visual Studio:
#ifdef STATS_LIBRARY_EXPORTS
# define LIBRARY_API __declspec(dllexport)
#else
# define LIBRARY_API __declspec(dllimport)
#endif
#include <windows.h>
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <time.h>
#include <bitset>
#include <stdio.h>
#include "C:\Cypress\Cypress Suite USB 3.4.7\CyAPI\inc\CyAPI.h"
_declspec(dllexport) int excite_LED(bool start, int on) {
int i;
USB_Open();
for(i=0; i<100; i++) // blink the LEDs for a few seconds
{
USB_BulkWrite(2, &i, 1); // send one single byte (= the value of i) to FIFO2
Sleep(50); // and wait 50ms
BulkOutPipe2->XferData((PUCHAR)&i, len); // send one byte (the value of i) to FIFO2
//Send command to FPGA
//status = !BulkOutPipe2->XferData(fpgaCommunicator, fpgaCommunicatorBytes);
}
USB_Close();
}
I am getting the following errors:
left of '->XferData' must point to class/struct/union/generic type
identifier "USB_Open" is undefined
identifier "USB_Close" is undefined
identifier "USB_BulkWrite" is undefined
identifier "len" is undefined
identifier "BulkOutPipe2" is undefined
cannot open source file "stdafx.h"
'USB_Open': identifier not found
'USB_Close': identifier not found
'USB_BulkWrite': identifier not found
'len': undeclared identifier
'BulkOutPipe2': undeclared identifier
How can I fix my code to get rid of these errors?
BulkOutPipe2 is a common define for EndPoint 3 in many samples, but its not set in CyAPI.h, so you need to set it yourself:
#define BulkOutPipe2 USBDevice->EndPoints[3]
Which you need to initialise:
CCyUSBDevice *USBDevice = new CCyUSBDevice(NULL, ...);
Len is also not defined:
LONG len= 512000;
USB_BulkWrite (and other USB_) looks like usb_bulk_write, which is part of the libusb api. But you are trying to use with the CyApi (which is a lot different), so remove those method calls.
Related
l learned "include" keyword are just copy & paste.
But including cpp file makes different compile result.
(gcc6~8 + boost1.69)
// main.cpp
#include <iostream>
// I'll move next code to why.cpp
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
void testFunc()
{
using namespace boost::archive::iterators;
typedef transform_width<binary_from_base64<remove_whitespace<std::string::const_iterator>>, 8, 6> ItBinaryT;
std::string input;
std::string output(ItBinaryT(input.begin()), ItBinaryT(input.end()));
}
// -----------------------------
int main()
{
return 0;
}
Above code compiled without warning.
But, I replace some code with include cpp..
// main.cpp
#include <iostream>
#include "why.cpp" // <----------
int main()
{
return 0;
}
// why.cpp - just copy&paste
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
void testFunc()
{
using namespace boost::archive::iterators;
typedef transform_width<binary_from_base64<remove_whitespace<std::string::const_iterator>>, 8, 6> ItBinaryT;
std::string input;
std::string output(ItBinaryT(input.begin()), ItBinaryT(input.end()));
}
It makes warning [-Wsubobject-linkage]
~~ has a field ~~ whose type uses the anonymous namespace
~~ has a base ~~ whose type uses the anonymous namespace
Please look at this link : https://wandbox.org/permlink/bw53IK2ZZP5UWMGk
What makes this difference?
Your compiler treats the main CPP file specially under the assumption that things defined in it are very unlikely to have more than one definition and so some tests for possible violation of the One Definition Rule are not done inside that file. Using #include takes you outside that file.
I would suggest just not using -Wsubobject-linkage since its logic is based on a heuristic that is not applicable to your code.
Here is the minimal C program to reproduce:
#include <alsa/asoundlib.h>
#include <sys/time.h>
int main( void )
{
}
This will compile with gcc -c -o timealsa.o timealsa.c, but if you include the -std=c99 switch, you get a redefinition error:
In file included from /usr/include/sys/time.h:28:0,
from timealsa.c:3:
/usr/include/bits/time.h:30:8: error: redefinition of ‘struct timeval’
struct timeval
^
In file included from /usr/include/alsa/asoundlib.h:49:0,
from timealsa.c:2:
/usr/include/alsa/global.h:138:8: note: originally defined here
struct timeval {
^
How can I resolve this conflict while still using -std=c99?
Since your question suggests you are using GLIBC's time.h there is a way to avoid this by telling it not to define timeval. Include asoundlib.h first then define _STRUCT_TIMEVAL. The one defined in asoundlib.h will be the one that gets used.
#include <alsa/asoundlib.h>
#ifndef _STRUCT_TIMEVAL
# define _STRUCT_TIMEVAL
#endif
#include <sys/time.h>
int main( void )
{
}
With C99 and later you can't have duplicate definitions of the same struct. The problem is that alsa/asoundlib.h includes alsa/global.h which contains this code:
/* for timeval and timespec */
#include <time.h>
...
#ifdef __GLIBC__
#if !defined(_POSIX_C_SOURCE) && !defined(_POSIX_SOURCE)
struct timeval {
time_t tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
#endif
#endif
So the Michael Petch's solution won't work - by the time you've included alsa/asoundlib.h it is already too late. The proper solution is to define _POSIX_C_SOURCE (_POSIX_SOURCE is obsolete). There's more information about these macros here and here.
For example you could try -D_POSIX_C_SOURCE=200809L. However, if you do that you'll get errors like this:
/usr/include/arm-linux-gnueabihf/sys/time.h:110:20: error: field ‘it_interval’ has incomplete type
struct timeval it_interval;
^
/usr/include/arm-linux-gnueabihf/sys/time.h:112:20: error: field ‘it_value’ has incomplete type
struct timeval it_value;
^
/usr/include/arm-linux-gnueabihf/sys/time.h:138:61: error: array type has incomplete element type
extern int utimes (const char *__file, const struct timeval __tvp[2])
^
This is all a big mess of old C code and macro madness. The only way I got it to work was to give up and use -std=gnu11.
I am playing around with using Semaphores, but I keep encountering Undefined Reference warnings, thus causing my code not to work. I pulled example code from a text, but was having issues with some of their syntax, so I went to POSIX's semaphore tutorial and changed things around to their syntax and as a result am now getting these reference errors.
I may simply be overlooking something, but I cannot find it.
Errors:
Producers_Consumers.c:52: warning: return type of ‘main’ is not ‘int’
/tmp/cceeOM6F.o: In function `producer':
Producers_Consumers.c:(.text+0x1e): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x3a): undefined reference to `sem_init'
Producers_Consumers.c:(.text+0x46): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x52): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x5e): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0x6a): undefined reference to `sem_post'
/tmp/cceeOM6F.o: In function `consumer':
Producers_Consumers.c:(.text+0x7e): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x8a): undefined reference to `sem_wait'
Producers_Consumers.c:(.text+0x96): undefined reference to `sem_post'
Producers_Consumers.c:(.text+0xa2): undefined reference to `sem_post'
collect2: ld returned 1 exit status
What I have (It may look a bit ugly due to the way I commented things out from my old method) I also know my adding method won't work, but I'll get to that when I fix my syntax issues:
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#define N 10 //Number of slots in buffer
typedef int semaphore; //Semaphores ae a special kind of int
sem_t mutex; //Controls access to critical region 1
sem_t empty; //Counts empty buffer slots N
sem_t full; //Counts full buffer slots 0
int count = 0; //What we're putting in
//int buffer[N];
void producer(void) {
sem_init(&mutex, 0, 1);
//sem_init(&empty, 0, N);
sem_init(&full, 0, 0);
while(1) {
sem_wait(&empty);
sem_wait(&mutex);
//printf("Empy: %d\n",empty);
//printf("Mutex: %d\n",mutex);
//printf("Both Downs Ran\n");
//buffer = buffer + 1;
sem_post(&mutex);
sem_post(&full);
//printf("Producer produced: %d\n",buffer);
}
}
void consumer(void) {
while(1) {
sem_wait(&full);
sem_wait(&mutex);
//item = buffer;
sem_post(&mutex);
sem_post(&empty);
//printf("Consumer consumed: %d/n",item);
}
}
void main() {
}
If you are on a linux system, you'll need to compile and link with the -pthread flag to link the pthreads library.
gcc -pthread Producers_Consumers.c
As Paul Griffiths has pointed out, you can also use -lrt, which is more portable, and links the POSIX Realtime Extensions library
gcc Producers_Consumers.c -lrt
Other notes specific to the code in the question:
int main(void) not void main()
typedef int semaphore is wrong, sem_t should be treated as an opaque type, you never use this typedef in your code anyway.
A problem I foresee is that your consumer code uses the semaphores before they are initialized in producer. You should initialize them in your main
Got same error in ubuntu qt.
After adding
LIBS += -lpthread -lrt
to project.pro file all compiled fine.
I'm trying to pass variable number of ofstreams over to a function that accepts an initializer_list but doesn't seem to work and throws all possible errors from the initializer_list structure and about how my function is with an array of ofstreams cannot be matched to any defined function.
Is it actually possible to pass a reference of ofstreams over in an initializer_list?
test.cpp
#include "extension.h"
ofstream outputFile, outputFile2;
int main(void) {
outputFile.open(("data_1.txt");
outputFile2.open("data_2.txt");
writeSomething({outputFile, outputFile2});
outputFile.close();
outputFile2.close();
}
extension.h
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string.h>
#include <initializer_list>
using namespace std;
void writeSomething(initializer_list<ofstream&> args);
extension.cpp
#include "extension.h"
void writeSomething(initializer_list<ofstream&> args) {
for (auto f : args ) {
f << "hello" << endl;
}
}
clang 3.4 (trunk 194324) produces a pretty clear error message:
initializer_list:54:23: error: 'iterator' declared as a pointer to a
reference of type
'std::basic_ofstream<char> &'
typedef const _E* iterator;
So no, it is not possible. See also Error: forming pointer to reference type 'const std::pair&'… I can't understand this error.
(gcc 4.7.2 and 4.8.1 crashes on this code due to some internal compiler error. I have submitted a bugreport.)
What you could do instead is to pass a pointer instead of a reference, something like this:
#include <fstream>
#include <initializer_list>
using namespace std;
void writeSomething(initializer_list<ofstream*> args) {
for (auto f : args )
*f << "hello" << endl;
}
int main() {
ofstream outputFile("data_1.txt");
ofstream outputFile2("data_2.txt");
writeSomething({&outputFile, &outputFile2});
}
However, I would much rather use a std::vector instead. Using an initializer list for this purpose is very strange and confusing for me.
Here is the simple echo.c source code:
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT(
"#(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "#(#)echo.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: echo.c,v 1.7 1997/07/20 06:07:03 thorpej Exp $");
#endif
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main __P((int, char *[]));
int
main(argc, argv)
int argc;
char *argv[];
{
/*
*main code with no error at all
*/
}
When compiling it with gcc 4.4.6, it report errors:
echo.c:4: error: expected declaration specifiers or â...â before string constant
echo.c:3: warning: data definition has no type or storage class
echo.c:12: error: expected declaration specifiers or â...â before string constant
echo.c:12: warning: data definition has no type or storage class
Line 3 and 4 is __COPYRIGHT macro.
Line 12 is __RCSID macro.
If I delete these two macro, it compiles successfully and runs correctly.
After some googling, I know that these two macros are defined in sys/cdefs.h and they are some kind of comment message.
But why it won't compile in gcc?
Well after going throuhg sys/cdefs.h (ubuntu 11.10), I found no __COPYRIGHT or __RCSID defination.
So I guess these two macros are defined in NetBSD sys/cdefs.h.
I added them in a new header file (I name it with "aeodefs.h") like the following:
#ifndef _AEODEFS_H_
#define _AEODEFS_H_
#include <sys/cdefs.h>
#define __IDSTRING(name,string) \
static const char name[] __attribute__((__unused__)) = string
#ifndef __RCSID
#define __RCSID(s) __IDSTRING(rcsid,s)
#endif
#ifndef __COPYRIGHT
#define __COPYRIGHT(s) __IDSTRING(copyright,s)
#endif
#endif /* !_AEODEFS_H_ */
Then change #include <sys/cdefs.h> to #include "aeodefs.h".
It's done!