以下是我编写的bash文件,用于将C文件中的所有C++样式(//)注释转换为C样式(/**/).
#!/bin/bash
lang=`echo $LANG`
# It's necessary to change the local setting. I don't know why.
export LANG=C
# Can comment the following statement if there is not dos2unix command.
dos2unix -q $1
sed -i -e 's;^\([[:blank:]]*\)//\(.*\);\1/* \2 */;' $1
export LANG=$lang
Run Code Online (Sandbox Code Playgroud)
有用.但我发现了一个我无法解释的问题.默认情况下,我的本地设置为en_US.UTF-8.在我的C代码中,有用中文写的评论,比如
// some english ??????
Run Code Online (Sandbox Code Playgroud)
如果我不更改本地设置,即不运行语句export LANG = C,我会得到
/* some english */??????
Run Code Online (Sandbox Code Playgroud)
代替
/* some english ??????*/
Run Code Online (Sandbox Code Playgroud)
我不知道为什么.我只是通过尝试和错误找到解决方案.
在阅读Jonathan Leffler的回答后,我认为我犯了一些错误导致了一些误解.在这个问题中,这些中文单词是在谷歌浏览器中输入的,而不是我的C文件中的实际单词.一些中文注释只是意味着一些中文评论.
现在我在Windows XP中的Visual C++ 6.0中输入了一些英文一些中文注释,并将c文件复制到Debian.然后我就运行sed -i -e's; ^([[:blank:]])//(.);\1 / …
我的C++代码如下:
//test.cpp
#include <iostream>
using namespace std;
template <int SIZE, class T>
struct A
{
void g(int x, const T& t)
{
t.f<SIZE>(x);
}
};
struct B
{
template <int SIZE>
void f(int x) const
{
cout << SIZE << ": " << x << endl;
}
};
int main(void)
{
A<3, B> a;
B b;
a.g(9, b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用g ++编译它时出现了一个令人惊讶的错误(版本是4.8.2):
test.cpp: In instantiation of ‘void A<SIZE, T>::g(int, const T&) [with int SIZE = 3; T …Run Code Online (Sandbox Code Playgroud) 如果此类是多态的,我发现类名不能隐藏在共享库中.例如,
// example.cpp
#include <stdio.h>
#include <string.h>
// #define virtual
class Base
{
public:
virtual const char* whatiam()
{
return "Papa";
}
};
class Child : public Base
{
public:
virtual const char* whatiam()
{
return "Son";
}
};
const char* whatiam(Base* obj)
{
return obj->whatiam();
}
__attribute__((visibility("default"))) const char* TheAPI(int n)
{
static char buf[64];
Child t;
sprintf(buf, "I'm %s.", whatiam(&t));
return buf;
}
Run Code Online (Sandbox Code Playgroud)
我在Linux上用这样的gcc构建一个共享库
$ g++ -fPIC -shared -fvisibility=hidden ../example.cpp -o libexample.so
$ strip -R .comment -R .note …Run Code Online (Sandbox Code Playgroud) 我想在编程中模拟退格键并按如下方式实现。
// del.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "123456";
cout << "\b\b\b" /* backspace key */<< '\x7f' /* DEL key */ << '\x7f' << '\x7f';
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我得到这样的结果

如何获得如下所示的结果而不需要用空格替换尾部
123
Run Code Online (Sandbox Code Playgroud)
也就是说如何删除而不是替换光标后退格的那些字符。
我用构建了opencv cmake -DWITH_OPENGL=ON ..,但是cmake的输出告诉我支持的OpenGL是NO。
而且我已经检查了cmake缓存以确保WITH_OPENGLis是ON。
使用的GUI是GTK + 3.0,并且已安装libgtkglext1-dev。