用g ++动态共享库编译(Dynamic Shared Library compilation with g++)
我正在尝试使用g ++编译以下简单的DL库示例代码,该程序来自Program-Library-HOWTO 。 这只是一个例子,所以我可以学习如何使用和编写共享库。 我正在开发的库的真实代码将用C ++编写。
#include <stdlib.h> #include <stdio.h> #include <dlfcn.h> int main(int argc, char **argv) { void *handle; double (*cosine)(double); char *error; handle = dlopen ("/lib/libm.so.6", RTLD_LAZY); if (!handle) { fputs (dlerror(), stderr); exit(1); } cosine = dlsym(handle, "cos"); if ((error = dlerror()) != NULL) { fputs(error, stderr); exit(1); } printf ("%f\n", (*cosine)(2.0)); dlclose(handle); }
如果我用gcc编译程序,它可以正常工作。
gcc -o foo foo.c -ldl
当我将文件名和编译器更改为以下内容时
g++ -o foo foo.cpp -ldl
我收到以下错误:
foo.cpp:16:错误:从'void *'无效转换为'double(*)(double)'
我明白(我想我理解,纠正我,如果这是错误的),我不能从C ++中的void指针隐式转换,但C让我,这就是为什么上面的代码将使用gcc编译但不使用克++。 所以我通过将上面的第16行改为:
cosine = (double *)dlsym(handle, "cos");
有了这个,我得到以下错误:
foo.cpp:16:错误:无法在赋值中将'double *'转换为'double(*)(double)'
这些问题可能与我自己对适当的C ++编码标准的一般无知比其他任何事情有关。 任何人都可以点我一个关于开发使用C ++示例代码的Linux动态库的好教程吗?
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with g++. This is just an example so I can learn how to use and write shared libraries. The real code for the library I'm developing will be written in C++.
#include <stdlib.h> #include <stdio.h> #include <dlfcn.h> int main(int argc, char **argv) { void *handle; double (*cosine)(double); char *error; handle = dlopen ("/lib/libm.so.6", RTLD_LAZY); if (!handle) { fputs (dlerror(), stderr); exit(1); } cosine = dlsym(handle, "cos"); if ((error = dlerror()) != NULL) { fputs(error, stderr); exit(1); } printf ("%f\n", (*cosine)(2.0)); dlclose(handle); }
If I compile the program with gcc it works fine.
gcc -o foo foo.c -ldl
When I change the filename and compiler to the following
g++ -o foo foo.cpp -ldl
I get the following error:
foo.cpp:16: error: invalid conversion from 'void*' to 'double (*)(double)'
I understand (I think I understand, correct me if this is wrong) that I can't do an implicit cast from a void pointer in C++, but C lets me, and this is why the above code will compile using gcc but not using g++. So I tried an explicit cast by changing line 16 above to:
cosine = (double *)dlsym(handle, "cos");
With this in place, I get the following error:
foo.cpp:16: error: cannot convert 'double*' to 'double (*)(double)' in assignment
These problems probably have more to do with my own general ignorance of proper C++ coding standards than anything else. Can anyone point me to a good tutorial on developing dynamic libraries for Linux that uses C++ example code?
原文:https://stackoverflow.com/questions/483797
最满意答案
我通过实现新的
UINavigationControllerDelegate
方法animationControllerForOperation
解决了这个问题。例如:
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { PushTransition* transition = [PushTransition new]; [transition setNavigationControllerOperation: operation]; return transition; }
PushTransition是一个实现
UIViewControllerAnimatedTransitioning
协议的类,以及该协议中的两个方法transitionDuration和animateTransition。 此外,我添加了一个属性来传递操作(告诉我它是否是推送或弹出过渡)。只需将动画代码移动到animateTransition中,如下所示:
// the containerView is the superview during the animation process. UIView *container = transitionContext.containerView; UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *fromView = fromVC.view; UIView *toView = toVC.view; CGFloat containerWidth = container.frame.size.width; // Set the needed frames to animate. CGRect toInitialFrame = [container frame]; CGRect fromDestinationFrame = fromView.frame; if ([self navigationControllerOperation] == UINavigationControllerOperationPush) { toInitialFrame.origin.x = containerWidth; toView.frame = toInitialFrame; fromDestinationFrame.origin.x = -containerWidth; } else if ([self navigationControllerOperation] == UINavigationControllerOperationPop) { toInitialFrame.origin.x = -containerWidth; toView.frame = toInitialFrame; fromDestinationFrame.origin.x = containerWidth; } // Create a screenshot of the toView. UIView *move = [toView snapshotViewAfterScreenUpdates:YES]; move.frame = toView.frame; [container addSubview:move]; [UIView animateWithDuration:TRANSITION_DURATION delay:0 usingSpringWithDamping:1000 initialSpringVelocity:1 options:0 animations:^{ move.frame = container.frame; fromView.frame = fromDestinationFrame; } completion:^(BOOL finished) { if (![[container subviews] containsObject:toView]) { [container addSubview:toView]; } toView.frame = container.frame; [fromView removeFromSuperview]; [move removeFromSuperview]; [transitionContext completeTransition: YES]; }];
描述它,你可以完成。 另外,你可以制作任何你喜欢的推动或流行动画。
I solved the problem by implementing the new
UINavigationControllerDelegate
MethodanimationControllerForOperation
.For example:
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { PushTransition* transition = [PushTransition new]; [transition setNavigationControllerOperation: operation]; return transition; }
PushTransition is a class that implements the
UIViewControllerAnimatedTransitioning
protocol and the two methods transitionDuration and animateTransition from that protocol. Additionally, i have added a property to pass the operation (tells me if it is a push or pop transition).Just put the animation code for moving the views into the animateTransition as follows:
// the containerView is the superview during the animation process. UIView *container = transitionContext.containerView; UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIView *fromView = fromVC.view; UIView *toView = toVC.view; CGFloat containerWidth = container.frame.size.width; // Set the needed frames to animate. CGRect toInitialFrame = [container frame]; CGRect fromDestinationFrame = fromView.frame; if ([self navigationControllerOperation] == UINavigationControllerOperationPush) { toInitialFrame.origin.x = containerWidth; toView.frame = toInitialFrame; fromDestinationFrame.origin.x = -containerWidth; } else if ([self navigationControllerOperation] == UINavigationControllerOperationPop) { toInitialFrame.origin.x = -containerWidth; toView.frame = toInitialFrame; fromDestinationFrame.origin.x = containerWidth; } // Create a screenshot of the toView. UIView *move = [toView snapshotViewAfterScreenUpdates:YES]; move.frame = toView.frame; [container addSubview:move]; [UIView animateWithDuration:TRANSITION_DURATION delay:0 usingSpringWithDamping:1000 initialSpringVelocity:1 options:0 animations:^{ move.frame = container.frame; fromView.frame = fromDestinationFrame; } completion:^(BOOL finished) { if (![[container subviews] containsObject:toView]) { [container addSubview:toView]; } toView.frame = container.frame; [fromView removeFromSuperview]; [move removeFromSuperview]; [transitionContext completeTransition: YES]; }];
described it and you can you are done. Additionally you can make any push or pop animation you like.
相关问答
更多-
我通过实现新的UINavigationControllerDelegate方法animationControllerForOperation解决了这个问题。 例如: - (id
)navigationController:(UINavigationController *)navigationController animationControllerForOper ... -
它的简单只是将iOS 6的UITableView背景设置为白色,而iOS 7则将其设置为清晰 if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { // Load resources for iOS 6.1 or earlier } else if (isPresentedWithPopOver) { // Load resources for iOS 7 or later } else { ...
-
它完全是关于Subviews的z顺序。 line.SendSubviewToBack (rectangle); 可能不是你想要的。 如果您添加具有正常红色的线条并添加其alpha降至例如0.5的UIView,那么您应该获得所需的效果。 The z-order of the subviews was correct in my case. Depending on the color the effect I want is possible or not. So I made the following: ...
-
Xcode 5具有专门用于解决此问题的iOS 6/7 Deltas 。 在故事板中,我将我的观点移动了20像素,看起来就像iOS 7,为了使iOS 6兼容,我将Delta y更改为-20。 由于我的故事板不使用自动布局,为了在iOS 6上正确调整视图的高度,我不得不设置Delta height以及Delta Y Xcode 5 has iOS 6/7 Deltas which is specifically made to resolve this issue. In the storyboard, I m ...
-
请在viewDidLoad方法中添加以下行: self.navigationController.navigationBar.translucent = YES; self.edgesForExtendedLayout = UIRectEdgeNone; Please add following lines in your viewDidLoad method: self.navigationController.navigationBar.translucent = YES; self.edgesF ...
-
如何知道一个移动视图(重叠)何时位于顶部(How to know when one moving view (overlaps) is on the top of the over)[2022-01-12]
我不知道库或框架,但您可以使用这两种方法来确定对象是否重叠( Rect.intersects方法执行大多数计算): private Rect getScreenBounds(View view) { int[] location = new int[2]; view.getLocationOnScreen(location); return new Rect(location[0], location[1], location[0] + view.getWidth(), locat ... -
如何在iOS7中调整UITabBarController内容视图的大小?(How to resize UITabBarController content view in iOS7?)[2022-02-10]
我设法通过这个到达iOS 7(在UIViewControllerWrapper内)的实际内容视图 UIView *viewToResize = [[transitionView.subviews[0] subviews] objectAtIndex:0]; 然后,当您调整该视图的大小时,您应该获得与调整UITransitionView大小相同的效果。 I managed to reach the actual content view on iOS 7 (inside the UIViewControll ... -
如何使文本视图透明(How to make Text View transparent)[2022-05-14]
点击文本视图和背景颜色,选择“清除颜色”。 Click on the Text view and for background color, choose "clear color". -
iOS 7视图控制器布局问题透明/模糊导航栏(iOS 7 view controller layout issue with transparent/blurred nav bar)[2021-10-31]
也许你可以尝试插入 [scroll setContentInset:UIEdgeInsetsMake(nav_bar_height + status_height, 0.0, 0.0, 0.0)]; [scroll setScrollIndicatorInsets:UIEdgeInsetsMake(nav_bar_height + status_height,0.0,0.0,0.0)]; 希望有所帮助 Maybe you can try with inset [scroll setContentInset ... -
从API级别21开始,按钮具有较高的高度,因此无论它们在View中的顺序如何,它们都显示在顶部。 您可以在视图中使用android:elevation" 。确保在您希望显示在顶部的视图中, android:elevation更高。 为什么不使用FrameLayout来绘制视图。 注意 - android:elevation"是在API级别21之后引入的。 From API level 21, Buttons have high elevation so they appear on top irrespec ...