I’am having trouble with const data.
It should be easy, but…
(working with ESP32 on PlatformIO)
This is OK:
*main.cpp*
const char test[] = { 1,2,3 };
main() {
char buff[10];
strcpy(buff, (char *) test);
}
…but if the const data are on another file, linker returns Undefined Reference to test:
*main.cpp*
extern const char test[];
main() {
char buff[10];
strcpy(buff, (char *) test);
}
--------------------------
*file.cpp*
const char test[] = { 1,2,3 };
Thanks for help!
You must include the file where you define your variables
Here is an example:
main.cpp
#include <pathTo/file.cpp>
main() {
char buff[10];
strcpy(buff, (char *) test);
}
file.cpp
#ifndef file_cpp
#define file_cpp
const char test[] = { 1,2,3 };
#endif
This bugged me also!
It seems that in C++ const variables not always get realized. To force a variable to be realized, add 'extern' also to its definition:
*file.cpp*
extern const char test[] = { 1,2,3 };
Worked for me on platform.io + ESP32
Related
I am trying to make compile time checks over the format of the printf. Here is the code.
#include <type_traits>
#include <iostream>
template <typename CHAR, typename ...ARGS>
constexpr size_t trace_cond(CHAR fmt, ARGS&&... args) {
//always needs to pass
return 1;
}
template <size_t N, typename ...ARGS>
constexpr size_t trace_cond(const char (&fmt)[N], ARGS&&... args) {
//return parse(fmt) == args;
return sizeof...(args) != 0;
}
#define TRACE(fmt, ...) { \
static_assert(trace_cond(fmt, __VA_ARGS__), "Wrong ARGS"); \
printf(fmt, ##__VA_ARGS__); \
}
int main(int argc, char* argv[]) {
//working fine
TRACE("%d %d\n", 2, 3);
const char* format = "%d %d\n";
//error
TRACE(format, 2, 3);
}
So when the format is known at compile time I want to have a check(using static_assert) and if it is not known then the check should be not called or always to pass
Obs:
Currently the code is not compiling because const char *format is not declared constexpr
The main should not be change because TRACE macro is used in a large codebase but changes to TRACE are more than welcome
So my question is:
Is there a way to skip static_assert or make it pass when fmt type is const char *
I think you can do what you want by moving the static_assert to the trace_cond
#include <type_traits>
#include <iostream>
template <size_t C, typename CHAR>
size_t trace_cond(CHAR fmt) {
//always needs to pass
return 1;
}
template <typename ...ARGS>
constexpr size_t count_args(ARGS&&... args) {
return sizeof...(args);
}
template <size_t C, size_t N>
constexpr size_t trace_cond(const char (&fmt)[N]) {
// TODO, parse fmt and do the needed checks
static_assert(C == 3, "");
return 1;
}
#define TRACE(fmt, ...) { \
trace_cond<count_args(__VA_ARGS__)>(fmt); \
printf(fmt, ##__VA_ARGS__); \
}
int main(int argc, char* argv[]) {
// Static check - hard coded to check for 3 arguments
TRACE("%d %d %d\n", 2, 3, 4);
const char* format = "%d %d\n";
// No checks performed
TRACE(format, 2, 3);
}
I'm using Tcl 8.6 and I'm trying to do something like this to add functions to the tcl interpreter
Tcl_Interp* interp,
void init() {
interp = Tcl_CreateInterp();
}
void add_tcl_function(char* cmd, function<int(int,char**)> F) {
obj2argv* o2a = new obj2argv;
auto lambda_proc = [&](
ClientData cdata,
Tcl_Interp* interp,
int objc,
Tcl_Obj* const objv[])
{
o2a->set(objc, objv);
F(objc, o2a->get_argv());
};
auto lamba_delete = [&](
delete o2a;
};
Tcl_CreateObjCommand(interp, cmd, lamda_proc, NULL, lamda_delete);
}
What I'm wondering is how to convert "Tcl_Obj* const objv[]" to "char** argv"?
I was thinking about creating a class:
class obj2argv {
obj2argv();
void set(int objc, Tcl_Obj* const objv[]);
char** get_argv();
private:
//...
};
any ideas on how to implement set() and get_argv()?
Is there an easier way to do this?
Thanks.
obj2argv* o2a = new obj2argv;
If you're interfacing a function that's fundamentally working with const char** for arguments, you should register the function with Tcl_CreateCommand and let Tcl handle the mapping to strings for you. It already has all the mechanisms required.
More formally, you are dealing with a gluing function with this signature:
typedef int (Tcl_CmdProc) (ClientData clientData, Tcl_Interp *interp,
int argc, CONST84 char *argv[]);
The CONST84 should be read as being plain const in all new code, and ClientData is a pointer-sized value that Tcl just hands around and never inspects (same as with your existing code).
If you are going to do the mapping yourself, Tcl_GetString takes a Tcl_Obj* and returns the char* representation of it. The representation should be usually treated as const; it simply isn't formally typed as such for historical reasons.
I wanted to add some more information:
I gave up on using lambda's because when I added capture list it won't convert the lambda to a function pointer for some reason. So I went with the traditional approach (see below). EXCEPT: I still have not idea why the TCL document says
typedef int Tcl_CmdProc(
ClientData clientData,
Tcl_Interp *interp,
int argc,
const char *argv[]);
But the compiler requires this to compile:
typedef int Tcl_CmdProc(
ClientData clientData,
Tcl_Interp *interp,
int argc,
Tcl_Obj* const* argv);
The Code:
int cmd_dispatch(
ClientData clientData,
Tcl_Interp* interp,
int argc,
Tcl_Obj* const* argv)
{
function<int(int,char**)> F = *(function<int(int,char**)>*)clientData;
return F(argc, (char**) argv); // <= CAST DOESN'T SEEM RIGHT
}
void cmd_delete(ClientData clientData)
{
}
void add_tcl_function(const char* cmd, function<int(int,char**)> F) {
Tcl_CreateObjCommand(interp, cmd, cmd_dispatch, (void*)&F, cmd_delete);
}
VERSION 2:
struct cmd_data {
//Tcl_Interp* interp,
function<int(int,char**)> F;
int argc;
char* argv[MAX_ARGS];
};
int cmd_dispatch(
ClientData clientData,
Tcl_Interp* interp,
int argc,
Tcl_Obj* const* objv)
{
auto cmd_data1 = (struct cmd_data*) clientData;
cmd_data1->argc = argc;
for(int i=0; ((i < argc) && (i < MAX_ARGS)); i++) {
cmd_data1->argv[i] = Tcl_GetString(objv[i]);
// Who owns object returned by Tcl_GetString?
// memory leak? or invalid after return from function?
// garbage collected by tcl interp?
}
return cmd_data1->F(argc, cmd_data1->argv);
}
void cmd_delete(ClientData clientData)
{
auto cmd_data1 = (struct cmd_data*) clientData;
if (cmd_data1) {
delete cmd_data1;
}
}
void add_tcl_function(const char* cmd, function<int(int,char**)> F) {
auto cmd_data1 = new struct cmd_data;
cmd_data1->F = F;
Tcl_CreateObjCommand(interp, cmd, cmd_dispatch, (void*)cmd_data1, cmd_delete);
}
void init_tcl_commands() {
auto lambda_hello = [&](int argc ,char** argv) -> int {
cout << "HELLO WORLD!\n";
return 0;
};
tcl_backend::add_tcl_function("hello", lambda_hello);
}
I'm trying to make C-string size calculation at compile time, using code like this:
#include <stdio.h>
#include <stdint.h>
class StringRef
{
public:
template<int N>
constexpr StringRef(const char (&str)[N])
: m_ptr(str), m_size(uint32_t(N-1)) {}
constexpr const char *constData() const
{ return m_ptr; }
private:
const char *m_ptr;
uint32_t m_size;
};
struct S
{
StringRef str;
};
constexpr static const struct S list[] =
{
"str",
};
int main()
{
printf("%s\n", list[0].str.constData());
return 0;
}
In clang-3.7 everything is fine, but in GCC 4.9.3-5.3 I get:
error: could not convert '(const char*)"str"' from 'const char*' to
'StringRef'
It can be fixed by adding explicit braces:
constexpr static const struct S list[] =
{{
{ "str" },
}};
But code became ugly and, still, clang somehow understand it correctly.
How can I make gcc understand array initialization without explicit braces?
I'm trying to learn how to use Bonjour using this blog article as a reference:
http://marknelson.us/2011/10/25/dns-service-discovery-on-windows/
I've download sample project linked at the bottom of that page, it works like charm. Now I'm trying to reproduce service discovery from scratch in my console application:
#include <iostream>
#include <assert.h>
#include "dns/dns_sd.h"
class CDnsSd
{
public:
bool discoverAsync ();
private:
static void DNSSD_API onDiscoveryFinished (DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
DNSServiceErrorType errorCode, const char *serviceName, const char *regtype,
const char *replyDomain, void *context);
};
bool CDnsSd::discoverAsync()
{
DNSServiceRef client = NULL;
const DNSServiceErrorType err = DNSServiceBrowse( &client, 0, 0, ""_services._dns-sd._udp"", "", onDiscoveryFinished, this );
return err == kDNSServiceErr_NoError;
}
void DNSSD_API CDnsSd::onDiscoveryFinished( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char *serviceName, const char *regtype, const char *replyDomain, void *context )
{
std::cout << __FUNCTION__;
}
void main ()
{
CDnsSd dnsSd;
const bool ret = dnsSd.discoverAsync();
assert(ret);
Sleep(10000000);
}
DNSServiceBrowse returns kDNSServiceErr_NoError, but the callback is never called. What's wrong?
You need a main loop processing Bonjour events. Look at the link you provided carefully. It's there in the "Driving the Callbacks" section.
I had to call the method named DNSServiceProcessResult to make it work for my part.
I hope it helped
I'm going through a source code analyzing its implementations where I have a method defined :
unsigned int rs_calc_weak_sum(void const *p, int len) {
unsigned char const *buf = (unsigned char const *) p;
}
What type of parameter should be passed into this method??
please help me.
thanks.
Any pointer can be passed to a void * parameter. What 'should' be passed, depends on what the code does with that parameter.
char array[12] = "Hello World";
unsigned in res = 0;
res = rs_calc_weak_sum(array, 12);
#include <stdio.h>
int main ( void )
{
char filename[] = "file.txt";
FILE *file = fopen ( filename, "r" );
if (file != NULL) {
char line [1000];
while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */ {
res = rs_calc_weak_sum(line, 1000);
}
fclose(file);
}
else {
perror(filename); //print the error message on stderr.
}
return 0;
}