Updateing code to swift 4 results in an error message - swift

I wan to update this code to swift 4:
rc = select(socket_fd + 1, readfd, writefd, NULL, &timeout);
return rc;
}
But I get two errors:
Declaration of 'select' must be imported from module 'Darwin.POSIX.sys.time' before it is required
Implicit declaration of function 'select' is invalid in C99
How can I can fix this?

Add this near the top of your file:
#include <sys/time.h>

Related

Failed to build LibcOverlayShims.h

I am attempting to build a framework that I just ported some code from Objective C to Swift in, and I'm getting this error:
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "LibcOverlayShims.h"
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/lib/swift/shims/LibcOverlayShims.h:80:15: error: declaration of 'sem_t' must be imported from module 'Darwin.sys.semaphore' before it is required
static inline sem_t *_stdlib_sem_open2(const char *name, int oflag) {
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/sys/semaphore.h:43:13: note: declaration here is not visible
typedef int sem_t;
^
<unknown>:0: error: could not build Objective-C module 'SwiftOverlayShims'
The error is happening because of this one file:
https://github.com/losnoco/Cog/blob/swiftingly/Audio/Visualization/VisualizationController.swift
What am I doing wrong?

pybind11, convert std::vector to py::list

According to the pybind11 documentation https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html:
When including the additional header file pybind11/stl.h, conversions between std::vector<>/std::list<>/std::array<>, std::set<>/std::unordered_set<>, and std::map<>/std::unordered_map<> and the Python list, set and dict data structures are automatically enabled.
However, I cannot for the life of me get this to work. I suppose I am misunderstanding something, so I hope someone can clear it up for me.
Here is what I expected to work:
// Test
std::vector<double> test_vec{1,2,3,4,5};
py::list test_list = test_vec;
py::list test_list2(test_vec);
py::list test_list3 = py::cast<py::list>(test_vec);
And here are the errors:
error: conversion from ‘std::vector<double>’ to non-scalar type ‘pybind11::list’ requested
py::list test_list = test_vec;
error: no matching function for call to ‘pybind11::list::list(std::vector<double>&)’
py::list test_list2(test_vec);
error: no matching function for call to ‘cast(std::vector<double>&)’
py::list test_list3 = py::cast<py::list>(test_vec)
The docs say to look in tests/test_stl.cpp for examples of how this should work, however I'm afraid that I am having trouble deciphering what is happening in that file.
The conversion happens automatically for function arguments and return values that you create bindings for if you include pybind11/stl.h. You can also do it explicitly in C++ code like this:
#include <pybind11/stl.h>
// [...]
std::vector<double> test_vec{1, 2, 3, 4, 5};
py::list test_list3 = py::cast(test_vec);
Please bear in mind that this creates a copy, though.
For a different approach please refer to making-opaque-types and binding-stl-containers.

no matching function for call to 'fopen'

I want to call fopen within my cpp function, however, Rcpp always complains about "no matching function for call to 'fopen'".
So duplicated exactly the some code from https://github.com/hadley/xml2/blob/9362d379e126a86091af8698a8987c51b5b230fe/src/xml2_doc.cpp and still have the same error.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void doc_write(std::string path){
FILE* f = fopen(path.c_str(), 'r');
fclose(f);
}
and the error is:
testc.cpp:6:36: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
Can someone point some hints on me?
The error message tells you everything you need to know - you're trying to pass a char parameter where a const char * is required.
replace 'r' with "r" that should fix it.

Eclipse undefined reference

I'm using Eclipse and MinGW. I've got undefined reference to error to all that I write in h files, that I do include in cpp-file where main located. I create an empty project, and the same thing again (
main.cpp
#include <iostream>
#include "Stack.h"
using namespace std;
int main(){
Stack<int> stack(10);
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
stack.h
#ifndef STACK_H_
#define STACK_H_
template <class T>
class Stack{
private:
struct StackEl;
StackEl *top;
public:
Stack();
Stack(T el);
~Stack();
void Push(const T& el);
T Pop();
};
#endif /* STACK_H_ */
and stack.cpp inplements everything from stack.h
If I include not h-file, but cpp - all works. Help please!
I've got following errors
D:/Workspacee/Stack2/Debug/../src/Stack2.cpp:16: undefined reference to `Stack<int>::Stack(int)'
D:/Workspacee/Stack2/Debug/../src/Stack2.cpp:18: undefined reference to `Stack<int>::~Stack()'
D:/Workspacee/Stack2/Debug/../src/Stack2.cpp:18: undefined reference to `Stack<int>::~Stack()'
This is a linker error. I'm no Eclipse expert, but you have to tell it somehow to add Stack.o to the linking command.
If you include Stack.cpp instead of Stack.h, the implementations from the cpp-file get included into main.cpp by the preprocessor before compilation, so the linking stage has no unresolved references to outside functions.
My bad, that is becouse templates! When you use template, all code, including realization of functions, must be in header-file, or you have to write prototypes for every type you are going to use you template-functions with. I've forgot about that working with templates is not the same as with usual function :(

G_DEFINE_TYPE error

In my gtk+ application i have following code:
G_DEFINE_TYPE(PicFile, pic_file, G_TYPE_OBJECT)
When i try to compile it i see error:
error:invalid application of sizeof to incomplete type 'PicFileClass'
Where PicFileClass -
typedef struct _PicFileFileClass PicFileClass;
struct _PicFileClass {
GObjectClass parent;
};
What's wrong?
Thank you.
In your first line you have _PicFileFileClass, while later it becomes _PicFileClass.