据我所知,编译时类C字符串只作为一个实例保存在静态内存中.例如,我true在下面的gcc 4.6运行示例中得到了两个.但我想知道它总是真的可以携带.C和C++上的行为很有趣.
#include <iostream>
bool amIportable(const char* value) {
const char* slocal = "Hello";
return (slocal==value);
}
int main() {
const char* s = "Hello";
std::cout << std::boolalpha
<< amIportable(s) << '\n'
<< amIportable("Hello") << '\n';
}
Run Code Online (Sandbox Code Playgroud) 我的项目通过几个静态库构建,这些库应该链接到主dll库,因此获得一个单独的dll.
使用__declspec(dllexport)属性不会导致静态库的指定函数出现到dll,库根本没有与dll链接.
然后我尝试将每个库构建为共享以获取导出函数的正确名称,并基于它们创建.def文件.使用.def文件导致结果.
应该__declspec(dllexport)和.def-file我的情况一样行事吗?
是否可以从源生成.def文件?由于我有C++代码,因为API中的修改和存在类,我不能自己编写.def文件,上面描述的临时生成的dll方法与生产不一致.
我想详细解释一下我项目的结构.该解决方案包含一些项目(模块).
+
|
+-+ static_lib1
| +
| +--+ src
|
+-+ static_lib2
| +
| +--+ src
|
+-+ dynamic_lib (linked with static_lib1 and static_lib2)
+
+--+ src
Run Code Online (Sandbox Code Playgroud)
每个子项目都弱于其他子项目,让我们假设它们之间没有联系是为了清晰.每个模块都有自己的公共接口.我想将所有模块作为单个动态库,所以我的工件是dynamic_lib.dll,但实际上静态库没有与它链接.
我没有发现C99标准中计数函数参数的任何限制,我想它只受堆栈大小的限制.
但是,我编写了一个简单的测试程序来演示具有大量参数的函数的行为.当它大约10k时,我在gcc上遇到以下错误(gg版本4.5.3在Cygwin上):
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../libcygwin.a(libcmain.o):(.text+0xa9): undefined reference to `_WinMain@16'
Run Code Online (Sandbox Code Playgroud)
我意识到如此大量的参数不太可能,但我想知道编译器的哪个参数决定了这个限制?
编辑
脚本生成C源代码
#!/bin/sh
num=$1
echo "" > out.c
echo "#include <stdio.h>" >> out.c
echo "int getsum( " >> out.c
i=0
while [ $i -lt $num ]
do
((i++))
if [ $i -eq $num ]
then
echo "int p$i )" >> out.c
else
echo -ne "int p$i," >> out.c
fi
done
echo "{" >> out.c
echo -ne " return " >> out.c
i=0
while [ $i -lt $num ]
do
((i++))
if [ …Run Code Online (Sandbox Code Playgroud) 目前,我MYPROJECT_CURRENT_HEADERS在CMake中使用一个变量来列出所有标题.当我使用Qt时,我的CMakeLists.txt包含:
QT4_WRAP_CPP(MYPROJECT_CURRENT_MOC ${MYPROJECT_CURRENT_HEADERS})
Run Code Online (Sandbox Code Playgroud)
问题是所有标头都由moc处理,即使那些没有Q_OBJECT:所以它生成许多空文件.
是否有"grep"/检测文件是否包含字符串的解决方案Q_OBJECT,如果是这种情况,请将其添加到MYPROJECT_CURRENT_MOC?
谢谢
感谢@flexo,当自由函数作为参数传递给另一个函数时,我对简单的回调没有任何问题.
但假设C接口比较困难:
typedef struct
{
int id;
const char* name;
} Item;
typedef struct
{
int value;
Items_Callback callback;
void *context;
} Items_Call;
typedef int (*Items_Callback)(const Item *item, void *context);
int Items_create(const Item *item, Items_Call *call) {
...
call->callback(item, call->context);
...
}
Run Code Online (Sandbox Code Playgroud)
我打算为这样的代码生成一些不错的Java包装器.我假设有结果
class Item {
public int id;
public String name;
}
class Items_Call {
public int value;
public Object context;
public Interface callback;
public void setInterface(Interface i){ callback=i; };
}
public interface Interface {
public int …Run Code Online (Sandbox Code Playgroud) 我有一个课我希望使用Google Mock进行模拟.我的班级有非虚拟和虚拟方法.我一直在读通过谷歌模拟傻瓜书和谷歌模拟食谱.这些资源提供的示例和解释提到具有ALL虚函数或NO虚函数的类,但不包含两者.所以我有两个问题:
(1)是否可以模拟具有混合虚拟/非虚拟类型的类?
(2)应该使用什么方法(如果问题1为真)来模拟这个类,(如果问题1是假的)可以使用什么方法呢?
一些代码,如果它有帮助:
class Time_Device : public Time_Device_Interface
{
private:
...
bool read32_irig_data( uint32_t *data_read, uint32_t reg_address);
bool thread_monitor_irig_changed( irig_callback_t callback );
public:
...
virtual bool set_time( struct time_sample const &time );
virtual bool get_time( struct time_sample *time );
virtual bool register_is_connected_notification(
irig_callback_t callback );
};
Run Code Online (Sandbox Code Playgroud)
一点点背景:
我正在尝试将谷歌模拟与谷歌测试一起使用,因为我需要在我的许多方法中模仿硬件返回以测试覆盖率等.我已经能够成功地单独使用Google测试来测试我的一些方法而不用嘲笑他们.
我正在使用Visual Studio 2010,CMake进行开发
我是Google Test和Google Mock的新手
我无法更改生产代码.
我将数据从nc输出存储在一个文件中24/7小时.它是温度的数据记录器.这个文件amroutput将会非常大.(每10秒一行).对于一个应用程序,我将在文件中有最后一个条目amr_last_output.所以我使用:
tail -f -n1 amroutput > amr_lastoutput.
Run Code Online (Sandbox Code Playgroud)
随着>我将在amr_lastoutput每次输出时覆盖文件.在mac osx上我们将输出尾部的输出附加到输出文件中,如>>命令中所示.我能做什么?
谢谢你的回答!
我希望以随机顺序从0到4得到数字,但相反,我有一些不同步的混乱
我做错了什么?
#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;
void addQuery(void *v );
HANDLE ghMutex;
int main()
{
HANDLE hs[5];
ghMutex = CreateMutex( NULL, FALSE, NULL);
for(int i=0; i<5; ++i)
{
hs[i] = (HANDLE)_beginthread(addQuery, 0, (void *)&i);
if (hs[i] == NULL)
{
printf("error\n"); return -1;
}
}
printf("WaitForMultipleObjects return: %d error: %d\n",
(DWORD)WaitForMultipleObjects(5, hs, TRUE, INFINITE), GetLastError());
return 0;
}
void addQuery(void *v )
{
int t = *((int*)v);
WaitForSingleObject(ghMutex, INFINITE);
cout << t << endl;
ReleaseMutex(ghMutex);
_endthread(); …Run Code Online (Sandbox Code Playgroud) 我上课了A.
A有自己的析构函数.
我A用来定义B如下.
class A{
protected:
int* array;
public:
A(int size){array = new int[size];}
~A() { delete [] array;}
}
class B{
public:
A x;
}
Run Code Online (Sandbox Code Playgroud)
我该如何定义析构函数B?
提前致谢.
我对最佳实践感到好奇.让我们假设我需要构造一个不应该被修改的对象:
void fn() {
const std::string& ref_to_const = "one";
...
const std::string const_object = "two";
...
}
Run Code Online (Sandbox Code Playgroud)
临时对象和const对象的生命周期都是相同的.那么使用什么方法?
我意识到我的例子一般不正确.但有趣的是找出它是如何工作的.
/* C/C++ (gcc-4.3.4) */
#include <stdio.h>
int main() {
/*volatile*/ int i = 5;
int j = 500;
int *p = &j;
printf( "%d %x\n", *p, p );
p++;
printf( "%d %x\n", *p, p ); // works correct with volatile (*p is 5)
//printf( "%d %x\n", *p, &i ); // works correct without volatile
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是某种优化吗?
UPDT 好的我得到了UB.我不希望别的别的.
但是,如果我有2个int vars,彼此相邻(见地址)为什么这个代码不起作用?