C++ and Objective-c - c++

i built a static library,and add the .h file to my project,
in all the class in the .h files,something like this :
class StartOp
{
static void openFiles(WavInFile **inFile, WavOutFile **outFile, const RunParameters *params);
static void setup(SoundTouch *pSoundTouch, const WavInFile *inFile, const RunParameters *params);
static void process(SoundTouch *pSoundTouch, WavInFile *inFile, WavOutFile *outFile);
static void detectBPM(WavInFile *inFile, RunParameters *params);
int start(const int nParams, const char * const paramStr[]);
};
i got this error:
StartOp.h:18: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'StartOp'
**EDIT**
i fix this problem by changing the file ex to mm.
now i want to use this code and i want to call from this class ,this code is an c++ class that i want to call him from an objective-c class:
#ifndef _STARTOP_H_
#define _STARTOP_H_
namespace soundtouch
{
class StartOp
{
static void openFiles(WavInFile **inFile, WavOutFile **outFile, const RunParameters *params);
static void setup(SoundTouch *pSoundTouch, const WavInFile *inFile, const RunParameters *params);
static void process(SoundTouch *pSoundTouch, WavInFile *inFile, WavOutFile *outFile);
static void detectBPM(WavInFile *inFile, RunParameters *params);
static int start(const int nParams, const char * const paramStr[]);
};
}
#endif
to the
static int start(const int nParams, const char * const paramStr[]);
method from my objective-c iphone app.

The code you posted is not C; it is rather C++.
Now, Objective-C and C++ can interoperate, but you need to use .mm extensions on the Objective-C side.
So if you are importing the .h. file into a .m file, you get obviously errors because the compiler is not expecting a class definition (C++). If you import the .h file into a .mm file, you have some chance that it will work (if all the rest is correct, I mean).
If you need more help, please provide more code, so that we can understand better what you are doing.

Related

multiple definition of a function

I defined a function to show a message when debug flags are off in a header file as below:
#ifdef NDEBUG
#define debug_msg(expr, msg) (static_cast<void>(0))
#else /* Not NDEBUG. */
#ifndef SHOW_DEBUG_H_
#define SHOW_DEBUG_H_
#include <stdio.h>
void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg)
{
printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
fflush(NULL);
}
#endif
#define debug_msg(expr, msg) \
((expr) \
? _show_in_debug(__FILE__, __LINE__, __func__, msg) \
: static_cast<void>(0))
#endif
when I include the header in more than a file, I get the following error:
multiple definition of `_show_in_debug(char const*, unsigned int, char
const*, char const*)'
I don't exactly know what I am doing wrong here, any help ?
Even with the include guards, you end up with a definition of _show_in_debug in each compilation unit. Linking those units then results to a multiple definition error.
For a debugging function like this, define the function as static so that it is not visible outside its compilation unit:
static void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg)
{
printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
fflush(NULL);
}
If you include that header into more than one .c file, each of them will define the function. That is what the error says.
What you should do is to only declare the function in the header (i.e. only put the prototype there; which you always should) and then define it in a single .c file (i.e. put the body in the .c file), encapsulated into the same switches as the prototype.
Header to be changed to this:
/* ... as you quoted ... */
void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg);
/* ... rest of what you quoted ... */
Code file to contain this:
#incude <stdio.h>
#include "Yourheader.h"
#ifdef NDEBUG
void _show_in_debug(const char *_file, unsigned int _line,
const char *_function, const char *_msg)
{
printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
fflush(NULL);
}
#endif
I assume the file header file that you have presented is included in several source files. Why is this a problem? That is because you have defined your _show_in_debug() function in the header instead of declaring it in the header and defining it in a source file file. This leads to the function be defined in multiple source files that have included your header.
See http://www.cprogramming.com/declare_vs_define.html for more details (especially, see the "Common Cases" section).

Template file not working correctly in CodeLite?

Every time I create a new project in my workplace I run into the problem with templates. For example, I'll create a new class, which CodeLite will create a .h file and a .cpp file for me, and then I'll change that .cpp file into a .template by renaming the file. It sometimes works, and sometimes doesn't. Sometimes I have to clean my workplace for it to work, other times I need to exit out of CodeLite and reopen it. This time these solutions are not working for me, but maybe I am missing something. Here's my code:
.h file
#ifndef TABLE1_H
#define TABLE1_H
#include <cstdlib> // Provides size_t
namespace main_savitch_12A
{
template <class RecordType>
class table
{
public:
// MEMBER CONSTANT -- See Appendix E if this fails to compile.
static const std::size_t CAPACITY = 811;
// CONSTRUCTOR
table( );
// MODIFICATION MEMBER FUNCTIONS
void insert(const RecordType& entry);
void remove(int key);
// CONSTANT MEMBER FUNCTIONS
bool is_present(int key) const;
void find(int key, bool& found, RecordType& result) const;
std::size_t size( ) const { return used; }
private:
// MEMBER CONSTANTS -- These are used in the key field of special records.
static const int NEVER_USED = -1;
static const int PREVIOUSLY_USED = -2;
// MEMBER VARIABLES
RecordType data[CAPACITY];
std::size_t used;
// HELPER FUNCTIONS
std::size_t hash(int key) const;
std::size_t next_index(std::size_t index) const;
void find_index(int key, bool& found, std::size_t& index) const;
bool never_used(std::size_t index) const;
bool is_vacant(std::size_t index) const;
};
}
#include "table1.template" // Include the implementation.
#endif
.template file
template<class RecordType>
table<RecordType>::table(){
used = 32;
}
main file
#include <stdio.h>
#include "table1.h"
int main(int argc, char **argv)
{
printf("hello world\n");
return 0;
}
My template and my .h files are called table1. The error I am getting when I run the program is in the template file. It reads: "table does not name a type" How can I fix this issue?
In your template implementation your are missing the namespace, use this:
template <class RecordType>
main_savitch_12A::table<RecordType>::table()
{
used = 32;
};

Apache module command parser prototype

I am creating an Apache2 module and experiencing a weird compilation problem.
This is prototype of my function used to parse config command named "analytics_ip":
static const char *apr_cfg_set_analytics_ip(cmd_parms *cmd, void *config, const char *data);
This is array of command_rec structures containing pointers to this function:
static const command_rec apr_cmds[] =
{
AP_INIT_TAKE1("analytics_ip", apr_cfg_set_analytics_ip, NULL, OR_ALL, ""),
{ NULL }
};
Structure command_rec is declared in header file http_config.h
typedef struct command_struct command_rec;
struct command_struct {
/** Name of this command */
const char *name;
/** The function to be called when this directive is parsed */
cmd_func func;
/** Extra data, for functions which implement multiple commands... */
void *cmd_data;
/** What overrides need to be allowed to enable this command. */
int req_override;
/** What the command expects as arguments */
enum cmd_how args_how;
/** 'usage' message, in case of syntax errors */
const char *errmsg;
};
When I follow cmd_func, it gets to the following declaration:
typedef const char *(*cmd_func) ();
If I am not mistaken, this means "pointer to function returning pointer to char and not accepting any arguments". How can this be possible? Command parsing function has to accept at least one parameter containing a module value of config variable corresponding to that function.
I am using g++ to compile this module.
Error message:
mod_xxx.h:65:2: error: invalid conversion from ‘const char* (*)(cmd_parms*, void*, const char*) {aka const char* (*)(cmd_parms_struct*, void*, const char*)}’ to ‘cmd_func {aka const char* (*)()}’ [-fpermissive]
};
Thanks in advance
cmd_func is a union, it is defined in http_config.h as follows:
typedef union {
/** function to call for a no-args */
const char *(*no_args) (cmd_parms *parms, void *mconfig);
/** function to call for a raw-args */
const char *(*raw_args) (cmd_parms *parms, void *mconfig,
const char *args);
/** function to call for a argv/argc */
const char *(*take_argv) (cmd_parms *parms, void *mconfig,
int argc, char *const argv[]);
/** function to call for a take1 */
const char *(*take1) (cmd_parms *parms, void *mconfig, const char *w);
/** function to call for a take2 */
const char *(*take2) (cmd_parms *parms, void *mconfig, const char *w,
const char *w2);
/** function to call for a take3 */
const char *(*take3) (cmd_parms *parms, void *mconfig, const char *w,
const char *w2, const char *w3);
/** function to call for a flag */
const char *(*flag) (cmd_parms *parms, void *mconfig, int on);
} cmd_func;
enum cmd_how args_how; is responsible for choosing the correct version of the function.
The switch handling it is located in server/config.c (in the invoke_cmd function).
You seem to be using the "take1" version which corresponds to cmd->AP_TAKE1 or simply cmd->take1.
The problem might be that C and C++ have differences regarding the union initialization. (AP_INIT_TAKE1 uses the { .take1=func } syntax which doesn't work in C++).
You'll have to initialize static const command_rec apr_cmds in a C++-compatible way or move it to a separate object file compiled with C. Or if you're not using C++ then simply compile with gcc.
For the project I'm working on we ended up adding a cast to allow the compilation to complete successfully, and the code seems to work OK as it correctly reads in the values specified in the configuration file. Here's the extract of this practice:
extern "C" {
static const command_rec kiwix_settings[] =
{
AP_INIT_TAKE1("zimFile", (const char* (*)())kiwix_set_zimfilename, NULL, RSRC_CONF, "The ZIM filename in full including the extension"),
AP_INIT_TAKE1("zimPath", (const char* (*)())kiwix_set_path, NULL, RSRC_CONF, "The path to the ZIM file, including the trailing //"),
{ NULL }
};
}
The full file (and indeed the project) are opensourced. Here's the link to the full file https://github.com/kiwix/kiwix-apache/blob/master/mod_kiwix.cpp
PS: thanks for your question and https://stackoverflow.com/users/257568/artemgr's answer as they helped me and another volunteer to work out how to resolve the problem for our project.

Define static method in source-file with declaration in header-file in C++

I am having a little trouble working with static methods in C++
Example .h:
class IC_Utility {
public:
IC_Utility();
~IC_Utility();
std::string CP_PStringToString( const unsigned char *outString );
void CP_StringToPString( std::string& inString, unsigned char *outString, short inMaxLength );
static void CP_StringToPString( std::string& inString, unsigned char *outString);
void CP_StringToPString( FxString& inString, FxUChar *outString);
};
Example .cpp:
static void IC_Utility::CP_StringToPString(std::string& inString, unsigned char *outString)
{
short length = inString.length();
if( outString != NULL )
{
if( length >= 1 )
CPLAT::CP_Utility::CP_CopyMemory( inString.c_str(), &outString[ 1 ], length );
outString[ 0 ] = length;
}
}
I wanted to make a call like:
IC_Utility::CP_StringToPString(directoryNameString, directoryName );
But I get an error:
error: cannot declare member function 'static void IC_Utility::CP_StringToPString(std::string&, unsigned char*)' to have static linkage
I dont understand why I cannot do this. Can anyone help me understand why and how to achieve what I want?
Remove static keyword in method definition. Keep it just in your class definition.
static keyword placed in .cpp file means that a certain function has a static linkage, ie. it is accessible only from other functions in the same file.
Keywords static and virtual should not be repeated in the definition. They should only be used in the class declaration.
You don't need to have static in function definition
Probably the best course of action is "do it as std lib does it". That is: All inline, all in headers.
// in the header
namespase my_namespace {
class my_standard_named_class final {
public:
static void standard_declared_defined_method () {
// even the comment is standard
}
} ;
} // namespase my_namespace
As simple as that ...
Static member functions must refer to static variables of that class. So in your case,
static void CP_StringToPString( std::string& inString, unsigned char *outString);
Since your member function CP_StringToPstring is static, the parameters in that function, inString and outString should be declared as static too.
The static member functions does not refer to the object that it is working on but the variables your declared refers to its current object so it return error.
You could either remove the static from the member function or add static while declaring the parameters you used for the member function as static too.

Where should I put my DECLSPEC for a namespace?

given the following namespace, where do I put my declspec (for dll export) i I want all the methods in said namespace to be exported into the DLL?
namespace AguiText {
void drawTextArea(AguiGraphicsManager *g, const AguiFont &font,const AguiRectangle &area,
const AguiColor &color, const std::vector<std::string> &lines,
AguiHorizontalAlignmentEnum horizontalAlignment, AguiVerticalAlignmentEnum verticalAlignment);
void divideText(std::vector<std::string> &words,
const std::string &text,const AguiFont &font, int maxWidth = -1);
void makeTextLines(const AguiFont &font,const std::vector<std::string> &words,
std::vector<std::string> &lineWords, int maxWidth );
void pointInTextArea(const AguiFont &font,
const AguiRectangle &area, const AguiPoint &point,
const std::vector<std::string> &lines, AguiRectangle &retRect,
int &retIndex, AguiHorizontalAlignmentEnum horizontalAlignment, AguiVerticalAlignmentEnum verticalAlignment);
}
Thanks
There is no need to export a namespace. From a compiled code perspective, namespaces are meaningless. You cannot reference them in code, they just define scope.
Namespaces wind up becoming part of class/function names during the name mangling process. They are not separate entities once a source file is compiled.
If you want to export the contents of a namespace, you need to export those entities, not the namespace itself.