Build Errors: LNK2019 & LNK1120 in QtVS 2015 Console Application project - visual-studio

**I have a C++ API and corresponding library(api-140.lib) file, using this I am developing a GUI project in a Microsoft Visual Studio-2015 with Qt. First of all I am checking the code in console application.
Before Build the project, I done the below things,
*Entered the IP Address in Command Line argument,
Added the library file path to Properties->Linker->Additional Library Dirctories &
Added the library file in Properties->Linker->Input->Additional Dependencies*
During Build the project I struct with Linker errors. I am newbie to Qt & MSVS. Kindly guide me to rectify these error.[enter image description here][1]
My System details are below:
Windows 10 64 bit
VS-2015, Qt-5.6.0(qt-opensource-windows-x86-msvc2015_64-5.6.0)**
My C++ code:
main.cpp
#include <string>
#include <stdint.h>
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <QtCore/QCoreApplication>
#include "qtcpserver.h"
#pragma comment (lib, "Ws2_32.lib")
#ifdef _UNICODE
std::string convertToString(_TCHAR* textW)
{
std::string result;
int lenA = WideCharToMultiByte(CP_ACP, 0, textW, -1, 0, 0, NULL, NULL);
printf("lenA = %d\n", lenA);
if (lenA > 0)
{
char* textA = new char[lenA + 1]; // allocate a final null terminator as well
printf("textA = %p\n", textA);
WideCharToMultiByte(CP_ACP, 0, textW, -1, textA, lenA, NULL, NULL);
printf("%d\n", __LINE__);
textA[lenA] = 0; // Set the null terminator yourself
result = textA;
delete[] textA;
}
return result;
}
#endif
#define GE_COMMON_VERSION "2.4.1"
#define GE_OK 0
#define GE_ERROR 1
#define GE_OVERFLOW 2
#define GE_INVALID_PARAMETER 3
#define GE_RESOURCE_UNAVAILABLE 4
#define GE_FILE_IN_USE 5
#define GE_ADDRESS_EXISTS 6
#define GE_INVALID_ADDRESS 7
#define GE_NOT_EXISTING 8
#define GE_UNDERRUN 9
#define GE_ABORT 10
#define GE_COMMFAIL 11
#define GE_BUSY 12
#define GE_NO_ACCESS 13
#define GE_NOT_PERMITTED 14
#define GEC_SFPDP_RECORDER_VERSION "3.5.0"
#define GEC_SFPDP_RECORDER_SERVER_UNIX_SOCKET_FILE "/tmp/ge-sfpdp-server"
#define GEC_SFPDP_RECORDER_SERVER_TCP_PORT 8001
#define GEC_SFPDP_NO_WAIT_FOR_SYNC 0
#define GEC_SFPDP_WAIT_FOR_ANY_SYNC 1
#define GEC_SFPDP_PLAYBACK_WITHOUT_SIGNALS 0
#define GEC_SFPDP_PLAYBACK_WITH_UNTIMED_SIGNALS 1
#define GEC_SFPDP_PLAYBACK_WITH_TIMED_SIGNALS 2
#define GEC_SFPDP_512_WORDS_CYCLE 0
#define GEC_SFPDP_16K_WORDS_CYCLE 1
#define GEC_SFPDP_512K_WORDS_CYCLE 2
#define GEC_SFPDP_16M_WORDS_CYCLE 3
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_0 (1 << 0)
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_1 (1 << 1)
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_2 (1 << 2)
#define GEC_SFPDP_INTERNAL_CHANNEL_SYNC_MASTER_3 (1 << 3)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT0 (1 << 4)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT1 (1 << 5)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT2 (1 << 6)
#define GEC_SFPDP_EXTERNAL_CHANNEL_SYNC_EXT3 (1 << 7)
#define GEC_SFPDP_AUTOMATIC_CONTROL 0
#define GEC_SFPDP_MANUAL_CONTROL 1
#define GEC_SFPDP_START_CHANNEL_SYNC 0
#define GEC_SFPDP_PAUSE_CHANNEL_SYNC 1
#define GEC_SFPDP_STOP_CHANNEL_SYNC 2
enum {
GEC_SFPDP_1_0625_GBPS,
GEC_SFPDP_2_125_GBPS,
GEC_SFPDP_2_5_GBPS,
GEC_SFPDP_3_125_GBPS,
GEC_SFPDP_4_25_GBPS
};
class GEC_ISfpdpRecorder
{
public:
virtual int setIntChannelSyncControl(int mode) = 0;
virtual int setIntChannelSync(uint32_t iDevice, int masterMask, int action) = 0;
virtual int setChannelSyncOutput(uint32_t iDevice, int signal, int source) = 0;
virtual int reset() = 0;
virtual int create(std::string path, std::string name, uint32_t iDevice, uint32_t iPort, uint32_t& id) = 0;
virtual int open(std::string path, std::string name, uint32_t iDevice, uint32_t iPort, int mode, uint32_t& id) = 0;
virtual int destroy(uint32_t id) = 0;
virtual int setCRC(uint32_t id, bool enableCRC) = 0;
virtual int setCopyMode(uint32_t id, bool enable) = 0;
virtual int setWaitForSync(uint32_t id, int mode) = 0;
virtual int setMaxSize(uint32_t id, uint64_t sizeInBytes) = 0;
virtual int setLinkSpeed(uint32_t id, int speed) = 0;
virtual int setFlowControl(uint32_t id, bool enable) = 0;
virtual int setRateControl(uint32_t id, int cycleSize, uint32_t cyclePartsForTx) = 0;
virtual int setSimplexLinkMode(uint32_t id, bool enable) = 0;
virtual int setChannelSyncSource(uint32_t id, int source) = 0;
virtual int setSwapping(uint32_t id, bool enable8in16, bool enable16in32) = 0;
virtual int getStatus(uint32_t id, uint64_t& numBytes, std::string& state) = 0;
virtual int checkForOverflow(uint32_t id, bool& detected, bool clear) = 0;
virtual int startAll() = 0;
virtual int stopAll() = 0;
};
class GEC_SfpdpRecorderFactory
{
public:
static GEC_ISfpdpRecorder* createTcpBasedInstance(std::string address);
static void destroyTcpBasedInstance(GEC_ISfpdpRecorder* instance);
};
int _tmain(int argc, _TCHAR* argv[])
{
QCoreApplication a(argc, argv);
if (argc < 2) {
printf("ERROR: Missing argument with the IP address of the SFPDP Record Manager Server\n");
return -1;
}
// Initialize Winsock
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result != 0) {
printf("WSAStartup failed with error: %d\n", result);
return -1;
}
int status = 0;
std::string ipAddress;
#ifdef _UNICODE
ipAddress = convertToString(argv[1]);
#else
ipAddress = argv[1];
#endif
GEC_ISfpdpRecorder* instance = GEC_SfpdpRecorderFactory::createTcpBasedInstance("ipAddress");
if (instance) {
int status = instance->reset();
printf("reset() returned %d as return code\n", status);
uint32_t id = 0;
status = instance->create("/data", "test", 0, 0, id);
printf("create() returned %d as return code\n", status);
printf(" and 0x%x as id\n", id);
status = instance->startAll();
printf("startAll() returned %d as return code\n", status);
uint64_t numBytesRecorded = 0;
std::string state;
status = instance->getStatus(id, numBytesRecorded, state);
printf("getStatus() returned %d as return code,\n", status);
printf(" %lld as number of recorded bytes,\n", numBytesRecorded);
printf(" and %s as state\n", state.c_str());
for (int i = 0; i < 12; ++i) {
Sleep(5000);
status = instance->getStatus(id, numBytesRecorded, state);
printf("getStatus() returned %d as return code,\n", status);
printf(" %lld as number of recorded bytes,\n", numBytesRecorded);
printf(" and %s as state\n", state.c_str());
}
status = instance->stopAll();
printf("stopAll() returned %d as return code\n", status);
status = instance->destroy(id);
printf("destroy() returned %d as return code\n", status);
GEC_SfpdpRecorderFactory::destroyTcpBasedInstance(instance);
}
WSACleanup();
return a.exec(),status;
}
Build Process:
1>------ Build started: Project: sfpdpgui1, Configuration: Debug x64 ------
1> Moc'ing sfpdpgui1.h...
1> moc_sfpdpgui1.cpp
1> main.cpp
1> sfpdpgui1.cpp
1> Generating Code...
1>main.obj : error LNK2019: unresolved external symbol "public: static class GEC_ISfpdpRecorder * __cdecl GEC_SfpdpRecorderFactory::createTcpBasedInstance(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?createTcpBasedInstance#GEC_SfpdpRecorderFactory##SAPEAVGEC_ISfpdpRecorder##V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function main
1>main.obj : error LNK2019: unresolved external symbol "public: static void __cdecl GEC_SfpdpRecorderFactory::destroyTcpBasedInstance(class GEC_ISfpdpRecorder *)" (?destroyTcpBasedInstance#GEC_SfpdpRecorderFactory##SAXPEAVGEC_ISfpdpRecorder###Z) referenced in function main
1>C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\x64\Debug\\sfpdpgui1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Build Errors:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: static class GEC_ISfpdpRecorder * __cdecl GEC_SfpdpRecorderFactory::createTcpBasedInstance(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?createTcpBasedInstance#GEC_SfpdpRecorderFactory##SAPEAVGEC_ISfpdpRecorder##V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function main sfpdpgui1 C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\sfpdpgui1\main.obj 1
Error LNK2019 unresolved external symbol "public: static void __cdecl GEC_SfpdpRecorderFactory::destroyTcpBasedInstance(class GEC_ISfpdpRecorder *)" (?destroyTcpBasedInstance#GEC_SfpdpRecorderFactory##SAXPEAVGEC_ISfpdpRecorder###Z) referenced in function main sfpdpgui1 C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\sfpdpgui1\main.obj 1
Error LNK1120 2 unresolved externals sfpdpgui1 C:\Users\Vignesh\Documents\Visual Studio 2015\Projects\sfpdpgui1\x64\Debug\\sfpdpgui1.exe 1
**Please find the link for Error image:**
[1]: https://i.stack.imgur.com/p2MY8.png

Related

Userfaultfd write protection appears unsupported when checking through the UFFDIO_API ioctl

I am trying to use the write protection feature of Linux's userfaultfd, but it does not appear to be enabled in my kernel even though I am using version 5.13 (write protection should be fully supported in 5.10+).
When I run
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
static int has_bit(uint64_t val, uint64_t bit) {
return (val & bit) == bit;
}
int main() {
long uffd; /* userfaultfd file descriptor */
struct uffdio_api uffdio_api;
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
uffdio_api.api = UFFD_API;
uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl-UFFDIO_API");
printf("UFFDIO_API: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_API));
printf("UFFDIO_REGISTER: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_REGISTER));
printf("UFFDIO_UNREGISTER: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_UNREGISTER));
printf("UFFDIO_WRITEPROTECT: %d\n", has_bit(uffdio_api.ioctls, 1UL << _UFFDIO_WRITEPROTECT));
printf("UFFD_FEATURE_PAGEFAULT_FLAG_WP: %d\n", has_bit(uffdio_api.features, UFFD_FEATURE_PAGEFAULT_FLAG_WP));
}
The output is
UFFDIO_API: 1
UFFDIO_REGISTER: 1
UFFDIO_UNREGISTER: 1
UFFDIO_WRITEPROTECT: 0
UFFD_FEATURE_PAGEFAULT_FLAG_WP: 1
The UFFD_FEATURE_PAGEFAULT_FLAG_WP feature is enabled, but the UFFDIO_WRITEPROTECT ioctl is marked as not supported, which is necessary to enable write protection.
What might lead to this feature being disabled, and how can I enable it?
I am using Ubuntu MATE 21.10 with Linux kernel version 5.13.0-30-generic.
EDIT:
It seems like despite the man page section on the UFFD_API ioctl (https://man7.org/linux/man-pages/man2/ioctl_userfaultfd.2.html), this might be the intended behavior for a system where write protection is enabled. However, when I run a full program that spawns a poller thread and writes to the protected memory, the poller thread does not receive any notification.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
static int page_size;
static void* fault_handler_thread(void* arg) {
long uffd; /* userfaultfd file descriptor */
uffd = (long) arg;
/* Loop, handling incoming events on the userfaultfd
file descriptor. */
for (;;) {
/* See what poll() tells us about the userfaultfd. */
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
nready = poll(&pollfd, 1, -1);
if (nready == -1)
errExit("poll");
printf("\nfault_handler_thread():\n");
printf(
" poll() returns: nready = %d; "
"POLLIN = %d; POLLERR = %d\n",
nready, (pollfd.revents & POLLIN) != 0,
(pollfd.revents & POLLERR) != 0);
// received fault, exit the program
exit(EXIT_FAILURE);
}
}
int main() {
long uffd; /* userfaultfd file descriptor */
char* addr; /* Start of region handled by userfaultfd */
uint64_t len; /* Length of region handled by userfaultfd */
pthread_t thr; /* ID of thread that handles page faults */
struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register;
struct uffdio_writeprotect uffdio_wp;
int s;
page_size = sysconf(_SC_PAGE_SIZE);
len = page_size;
/* Create and enable userfaultfd object. */
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
uffdio_api.api = UFFD_API;
uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl-UFFDIO_API");
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED)
errExit("mmap");
printf("Address returned by mmap() = %p\n", addr);
/* Register the memory range of the mapping we just created for
handling by the userfaultfd object. */
uffdio_register.range.start = (unsigned long) addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl-UFFDIO_REGISTER");
printf("uffdio_register.ioctls = 0x%llx\n", uffdio_register.ioctls);
printf("Have _UFFDIO_WRITEPROTECT? %s\n", (uffdio_register.ioctls & _UFFDIO_WRITEPROTECT) ? "YES" : "NO");
uffdio_wp.range.start = (unsigned long) addr;
uffdio_wp.range.len = len;
uffdio_wp.mode = UFFDIO_WRITEPROTECT_MODE_WP;
if (ioctl(uffd, UFFDIO_WRITEPROTECT, &uffdio_wp) == -1)
errExit("ioctl-UFFDIO_WRITEPROTECT");
/* Create a thread that will process the userfaultfd events. */
s = pthread_create(&thr, NULL, fault_handler_thread, (void*) uffd);
if (s != 0) {
errno = s;
errExit("pthread_create");
}
/* Main thread now touches memory in the mapping, touching
locations 1024 bytes apart. This will trigger userfaultfd
events for all pages in the region. */
usleep(100000);
size_t l;
l = 0xf; /* Ensure that faulting address is not on a page
boundary, in order to test that we correctly
handle that case in fault_handling_thread(). */
char i = 0;
while (l < len) {
printf("Write address %p in main(): ", addr + l);
addr[l] = i++;
printf("%d\n", addr[l]);
l += 1024;
usleep(100000); /* Slow things down a little */
}
exit(EXIT_SUCCESS);
}
The UFFD_API ioctl does not seem to ever report _UFFD_WRITEPROTECT as can be seen here in the kernel source code (1, 2). I assume that this is because whether this operation is supported or not depends on the kind of underlying mapping.
The feature is in fact reporeted on a per-registered-range basis. You will have to set the API with ioctl(uffd, UFFDIO_API, ...) first, then register a range with ioctl(uffd, UFFDIO_REGISTER, ...) and then check the uffdio_register.ioctls field.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
int main(void) {
long uffd;
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
struct uffdio_api uffdio_api = { .api = UFFD_API };
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl(UFFDIO_API)");
const size_t region_sz = 0x4000;
void *region = mmap(NULL, region_sz, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
if (region == MAP_FAILED)
errExit("mmap");
if (posix_memalign((void **)region, sysconf(_SC_PAGESIZE), region_sz))
errExit("posix_memalign");
printf("Region mapped at %p - %p\n", region, region + region_sz);
struct uffdio_register uffdio_register = {
.range = { .start = (unsigned long)region, .len = region_sz },
.mode = UFFDIO_REGISTER_MODE_WP
};
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl(UFFDIO_REGISTER)");
printf("uffdio_register.ioctls = 0x%llx\n", uffdio_register.ioctls);
printf("Have _UFFDIO_WRITEPROTECT? %s\n", (uffdio_register.ioctls & _UFFDIO_WRITEPROTECT) ? "YES" : "NO");
if ((uffdio_register.ioctls & UFFD_API_RANGE_IOCTLS) != UFFD_API_RANGE_IOCTLS)
errExit("bad ioctl set");
struct uffdio_writeprotect wp = {
.range = { .start = (unsigned long)region, .len = region_sz },
.mode = UFFDIO_WRITEPROTECT_MODE_WP
};
if (ioctl(uffd, UFFDIO_WRITEPROTECT, &wp) == -1)
errExit("ioctl(UFFDIO_WRITEPROTECT)");
puts("ioctl(UFFDIO_WRITEPROTECT) successful.");
return EXIT_SUCCESS;
}
Output:
Region mapped at 0x7f45c48fe000 - 0x7f45c4902000
uffdio_register.ioctls = 0x5c
Have _UFFDIO_WRITEPROTECT? YES
ioctl(UFFDIO_WRITEPROTECT) successful.
I found the solution. The write-protected pages must be touched after registering but before marking them as write-protected. This is an undocumented requirement, from what I can tell.
In other words, add
for (size_t i = 0; i < len; i += page_size) {
addr[i] = 0;
}
between registering and write-protecting.
It works if I change the full example to
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/userfaultfd.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#define errExit(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
static int page_size;
static void* fault_handler_thread(void* arg) {
long uffd; /* userfaultfd file descriptor */
uffd = (long) arg;
/* Loop, handling incoming events on the userfaultfd
file descriptor. */
for (;;) {
/* See what poll() tells us about the userfaultfd. */
struct pollfd pollfd;
int nready;
pollfd.fd = uffd;
pollfd.events = POLLIN;
nready = poll(&pollfd, 1, -1);
if (nready == -1)
errExit("poll");
printf("\nfault_handler_thread():\n");
printf(
" poll() returns: nready = %d; "
"POLLIN = %d; POLLERR = %d\n",
nready, (pollfd.revents & POLLIN) != 0,
(pollfd.revents & POLLERR) != 0);
// received fault, exit the program
exit(EXIT_FAILURE);
}
}
int main() {
long uffd; /* userfaultfd file descriptor */
char* addr; /* Start of region handled by userfaultfd */
uint64_t len; /* Length of region handled by userfaultfd */
pthread_t thr; /* ID of thread that handles page faults */
struct uffdio_api uffdio_api;
struct uffdio_register uffdio_register;
struct uffdio_writeprotect uffdio_wp;
int s;
page_size = sysconf(_SC_PAGE_SIZE);
len = page_size;
/* Create and enable userfaultfd object. */
uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (uffd == -1)
errExit("userfaultfd");
uffdio_api.api = UFFD_API;
uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
if (ioctl(uffd, UFFDIO_API, &uffdio_api) == -1)
errExit("ioctl-UFFDIO_API");
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED)
errExit("mmap");
printf("Address returned by mmap() = %p\n", addr);
/* Register the memory range of the mapping we just created for
handling by the userfaultfd object. */
uffdio_register.range.start = (unsigned long) addr;
uffdio_register.range.len = len;
uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
if (ioctl(uffd, UFFDIO_REGISTER, &uffdio_register) == -1)
errExit("ioctl-UFFDIO_REGISTER");
printf("uffdio_register.ioctls = 0x%llx\n", uffdio_register.ioctls);
printf("Have _UFFDIO_WRITEPROTECT? %s\n", (uffdio_register.ioctls & _UFFDIO_WRITEPROTECT) ? "YES" : "NO");
for (size_t i = 0; i < len; i += page_size) {
addr[i] = 0;
}
uffdio_wp.range.start = (unsigned long) addr;
uffdio_wp.range.len = len;
uffdio_wp.mode = UFFDIO_WRITEPROTECT_MODE_WP;
if (ioctl(uffd, UFFDIO_WRITEPROTECT, &uffdio_wp) == -1)
errExit("ioctl-UFFDIO_WRITEPROTECT");
/* Create a thread that will process the userfaultfd events. */
s = pthread_create(&thr, NULL, fault_handler_thread, (void*) uffd);
if (s != 0) {
errno = s;
errExit("pthread_create");
}
/* Main thread now touches memory in the mapping, touching
locations 1024 bytes apart. This will trigger userfaultfd
events for all pages in the region. */
usleep(100000);
size_t l;
l = 0xf; /* Ensure that faulting address is not on a page
boundary, in order to test that we correctly
handle that case in fault_handling_thread(). */
char i = 0;
while (l < len) {
printf("Write address %p in main(): ", addr + l);
addr[l] = i++;
printf("%d\n", addr[l]);
l += 1024;
usleep(100000); /* Slow things down a little */
}
exit(EXIT_SUCCESS);
}

Not able to send info to LCD

I studied this code in one of the youtube tutorials, I cannot send info to LCD, the program shows error, the error which I keep getting is
Initializing argument 1 of 'void Send_A_String(char*)' [-fpermissive]
initializing argument 3 of 'void Send_An_IntegerToMrLCD(uint8_t, uint8_t, int, char)' [-fpermissive]
invalid conversion from 'char' to 'char*' [-fpermissive]
invalid conversion from 'char*' to 'int' [-fpermissive]
Below is the header file for LCD - MrLCD.h
#ifndef MrLCD
#define MrLCD
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#define MrLCDsCrib PORTB
#define DataDir_MrLCDsCrib DDRB
#define MrLCDsControl PORTD
#define DataDir_MrLCDsControl DDRD
#define LightSwitch 5
#define ReadWrite 7
#define BiPolarMood 2
char firstColumnPositionsForMrLCD[4] = {0, 64, 20, 84};
void Check_IF_MrLCD_isBusy(void);
void Peek_A_Boo(void);
void Send_A_Command(unsigned char command);
void Send_A_Character(unsigned char character);
void Send_A_String(char *StringOfCharacters);
void initializeMrLCD(void);
void GotoMrLCDsLocation(uint8_t x, uint8_t y);
void Send_A_StringToMrLCDWithLocation(uint8_t x, uint8_t y, char *StringOfCharacters);
void Send_An_IntegerToMrLCD(uint8_t x, uint8_t y, int IntegerToDisplay, char NumberOfDigits);
void Check_IF_MrLCD_isBusy()
{
DataDir_MrLCDsCrib = 0;
MrLCDsControl |= 1<<ReadWrite;
MrLCDsControl &= ~1<<BiPolarMood;
while (MrLCDsCrib >= 0x80)
{
Peek_A_Boo();
}
DataDir_MrLCDsCrib = 0xFF;
}
void Peek_A_Boo()
{
MrLCDsControl |= 1<<LightSwitch;
asm volatile ("nop");
asm volatile ("nop");
MrLCDsControl &= ~1<<LightSwitch;
}
void Send_A_Command(unsigned char command)
{
Check_IF_MrLCD_isBusy();
MrLCDsCrib = command;
MrLCDsControl &= ~ ((1<<ReadWrite)|(1<<BiPolarMood));
Peek_A_Boo();
MrLCDsCrib = 0;
}
void Send_A_Character(unsigned char character)
{
Check_IF_MrLCD_isBusy();
MrLCDsCrib = character;
MrLCDsControl &= ~ (1<<ReadWrite);
MrLCDsControl |= 1<<BiPolarMood;
Peek_A_Boo();
MrLCDsCrib = 0;
}
void Send_A_String(char *StringOfCharacters)
{
while(*StringOfCharacters > 0)
{
Send_A_Character(*StringOfCharacters++);
}
}
void initializeMrLCD()
{
DataDir_MrLCDsControl |= 1<<LightSwitch | 1<<ReadWrite | 1<<BiPolarMood;
_delay_ms(15);
Send_A_Command(0x01); //Clear Screen 0x01 = 00000001
_delay_ms(2);
Send_A_Command(0x38);
_delay_us(50);
Send_A_Command(0b00001110);
_delay_us(50);
}
void GotoMrLCDsLocation(uint8_t x, uint8_t y)
{
Send_A_Command(0x80 + firstColumnPositionsForMrLCD[y-1] + (x-1));
}
void Send_A_StringToMrLCDWithLocation(uint8_t x, uint8_t y, char *StringOfCharacters)
{
GotoMrLCDsLocation(x, y);
Send_A_String(*StringOfCharacters);
}
void Send_An_IntegerToMrLCD(uint8_t x, uint8_t y, int IntegerToDisplay, char NumberOfDigits)
{
char StringToDisplay[NumberOfDigits];
itoa(IntegerToDisplay, StringToDisplay, 10);
Send_A_StringToMrLCDWithLocation(x, y, StringToDisplay);
Send_A_String(" ");
}
#endif
And in the simplest of programs in the main file, if I try to call these functions in the header file like Send_A_StringToMrLCDWithLocation or Send_An_IntegerToMrLCD... The program shows error...
You need to move all this code into a C file, i.e. MrLCD.c because that is where the code is instantiated.
You have not included the .c file where you call these functions, but the error appears to say that you are calling the functions with the wrong type of variables.
You are calling void Send_A_String(char*) with a char rather than a pointer to a string.
You are calling void Send_An_IntegerToMrLCD(uint8_t, uint8_t, int, char) with a char pointer (string) rather than an int in the third parameter.

usb_libusb10.h(72): error C2146: syntax error : missing ';' before identifier 'cb'

I am getting the following error even though it seems there are no syntax errors.
usb_libusb10.h(72): error C2146: syntax error : missing ';' before identifier 'cb'
The line where the error is generating is given below-
fnusb_iso_cb cb;
I also tried to highlighted this line by using "**" in the code.
Following is the code I am using.
#pragma once
#include <Windows.h>
#include <afxwin.h>
#include "libfreenect.h"
#include <libusb.h>
// There are a few rules: PKTS_PER_XFER * NUM_XFERS <= 1000, PKTS_PER_XFER % 8 == 0.
#if defined(__APPLE__)
#define DEPTH_PKTBUF 2048
#define VIDEO_PKTBUF 2048
#define PKTS_PER_XFER 128
#define NUM_XFERS 4
#else
#define DEPTH_PKTBUF 1920
#define VIDEO_PKTBUF 1920
#if defined(_WIN32)
#define PKTS_PER_XFER 32
#define NUM_XFERS 8
#else
#define PKTS_PER_XFER 16
#define NUM_XFERS 16
#endif
#endif
typedef struct {
libusb_context *ctx;
int should_free_ctx;
} fnusb_ctx;
typedef struct {
freenect_device *parent; //so we can go up from the libusb userdata
libusb_device_handle *dev;
int device_dead; // set to 1 when the underlying libusb_device_handle vanishes (ie, Kinect was unplugged)
int VID;
int PID;
} fnusb_dev;
typedef struct {
fnusb_dev *parent; //so we can go up from the libusb userdata
struct libusb_transfer **xfers;
uint8_t *buffer;
**fnusb_iso_cb cb;**
int num_xfers;
int pkts;
int len;
int dead;
int dead_xfers;
} fnusb_isoc_stream;
int fnusb_num_devices(fnusb_ctx *ctx);
int fnusb_list_device_attributes(fnusb_ctx *ctx, struct freenect_device_attributes** attribute_list);
int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx);
int fnusb_shutdown(fnusb_ctx *ctx);
int fnusb_process_events(fnusb_ctx *ctx);
int fnusb_process_events_timeout(fnusb_ctx *ctx, struct timeval* timeout);
int fnusb_open_subdevices(freenect_device *dev, int index);
int fnusb_close_subdevices(freenect_device *dev);
int fnusb_start_iso(fnusb_dev *dev, fnusb_isoc_stream *strm, fnusb_iso_cb cb, unsigned char endpoint, int xfers, int pkts, int len);
int fnusb_stop_iso(fnusb_dev *dev, fnusb_isoc_stream *strm);
int fnusb_get_max_iso_packet_size(fnusb_dev *dev, unsigned char endpoint, int default_size);
int fnusb_control(fnusb_dev *dev, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength);
int fnusb_bulk(fnusb_dev *dev, uint8_t endpoint, uint8_t *data, int len, int *transferred);
int fnusb_num_interfaces(fnusb_dev *dev);

Missing linux/if.h

hello I am getting this error when using cygwin to try to make,
this is my makefile
flags=-g
all: icmptx
icmptx: it.o icmptx.o tun_dev.o
gcc $(flags) -o icmptx icmptx.o it.o tun_dev.o
it.o: it.c
gcc $(flags) -c it.c
icmptx.o: icmptx.c
gcc $(flags) -c icmptx.c
tun_dev.o: tun_dev.c
gcc $(flags) -c tun_dev.c
clean:
rm -f tun_dev.o it.o icmptx.o icmptx
the error is
$ make
gcc -g -c tun_dev.c
tun_dev.c:35:22: fatal error: linux/if.h: No such file or directory
compilation terminated.
Makefile:15: recipe for target `tun_dev.o' failed
make: *** [tun_dev.o] Error 1
here is my tun_dev.c file
/*
*/
/*
* tun_dev.c,v 1.1.2.4 2001/09/13 05:02:22 maxk Exp
*/
/* #include "config.h" */
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include "vtun.h"
#include "lib.h"
/*
* Allocate TUN device, returns opened fd.
* Stores dev name in the first arg(must be large enough).
*/
int tun_open_old(char *dev)
{
char tunname[14];
int i, fd;
if( *dev ) {
sprintf(tunname, "/dev/%s", dev);
return open(tunname, O_RDWR);
}
for(i=0; i < 255; i++){
sprintf(tunname, "/dev/tun%d", i);
/* Open device */
if( (fd=open(tunname, O_RDWR)) > 0 ){
sprintf(dev, "tun%d", i);
return fd;
}
}
return -1;
}
#include <linux/if_tun.h>
/* pre 2.4.6 compatibility */
#define OTUNSETNOCSUM (('T'<< 8) | 200)
#define OTUNSETDEBUG (('T'<< 8) | 201)
#define OTUNSETIFF (('T'<< 8) | 202)
#define OTUNSETPERSIST (('T'<< 8) | 203)
#define OTUNSETOWNER (('T'<< 8) | 204)
int tun_open(char *dev)
{
struct ifreq ifr;
int fd;
if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
return tun_open_old(dev);
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if (*dev)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0) {
if (errno == EBADFD) {
/* Try old ioctl */
if (ioctl(fd, OTUNSETIFF, (void *) &ifr) < 0)
goto failed;
} else
goto failed;
}
strcpy(dev, ifr.ifr_name);
return fd;
failed:
close(fd);
return -1;
}
int tun_close(int fd, char *dev)
{
return close(fd);
}
/* Read/write frames from TUN device */
int tun_write(int fd, char *buf, int len)
{
return write(fd, buf, len);
}
int tun_read(int fd, char *buf, int len)
{
return read(fd, buf, len);
}
any idea, google shows nothing
Try:
#include <net/if.h>
instead.

CreateThread() error

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
void Thread1( LPVOID param)
{
int a;
a = *((int *)param);
for (int i= 0; i <10; i++)
printf("%d\n", a);
}
int main()
{
int a =4;
int ThreadId;
CreateThread( 0, 0x0100, Thread1, &a, 0, &ThreadId);
for( int i = 0; i <11; i++)
Sleep( 1);
return( 1);
}
This is a simple code but I am not able to figure it out why visual studio is giving me error:
error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (void *)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
Error executing cl.exe.
define as following
DWORD WINAPI MyThreadProc(LPVOID lpParameter)
CreateThread() require __stdcall calling convention.

Resources