如果我包含<stdlib.h>或<stdio.h>在C程序中,我不必在编译时链接这些,但我必须链接到<math.h>,使用-lmgcc,例如:
gcc test.c -o test -lm
Run Code Online (Sandbox Code Playgroud)
这是什么原因?为什么我必须显式链接数学库而不是其他库?
我正在使用arm-linux-androideabi-g++编译器.当我尝试编译一个简单的"你好,世界!" 程序编译好.当我通过在该代码中添加一个简单的异常处理来测试它时它也可以工作(添加之后-fexceptions..我猜它默认是禁用的).
这适用于Android设备,我只想使用CMake,而不是ndk-build.
例如 - first.cpp
#include <iostream>
using namespace std;
int main()
{
try
{
}
catch (...)
{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
./arm-linux-androideadi-g++ -o first-test first.cpp -fexceptions
它没有问题......
问题 ...我试图用CMake文件编译文件.
我想添加-fexceptions标志.我试过了
set (CMAKE_EXE_LINKER_FLAGS -fexceptions ) or set (CMAKE_EXE_LINKER_FLAGS "fexceptions" )
Run Code Online (Sandbox Code Playgroud)
和
set ( CMAKE_C_FLAGS "fexceptions")
Run Code Online (Sandbox Code Playgroud)
它仍然显示错误.
我正在尝试编译multiple_sources.cpp以在我的计算机上编译.我正在运行Xubuntu Lucid Lynx全面更新.
它将编译没有问题,g++ -c multiple_sources.cpp但当我尝试链接并使g++ multiple_sources.o我得到一个exectuable 得到:
multiple_sources.cpp:(.text+0x3d): undefined reference to `boost::program_options::options_description::m_default_line_length'
multiple_sources.cpp:(.text+0x87): undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)'
multiple_sources.cpp:(.text+0x15a): undefined reference to `boost::program_options::options_description::add_options()'
multiple_sources.cpp:(.text+0x17b): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, char const*)'
multiple_sources.cpp:(.text+0x193): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, char const*)'
multiple_sources.cpp:(.text+0x1af): undefined reference to `boost::program_options::options_description_easy_init::operator()(char const*, boost::program_options::value_semantic const*, char const*)'
multiple_sources.cpp:(.text+0x1eb): undefined reference to `boost::program_options::options_description::m_default_line_length'
multiple_sources.cpp:(.text+0x252): undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)'
...
Run Code Online (Sandbox Code Playgroud)
et cetera …
我尝试在Clion IDE中编写代码(c/c ++).我需要在项目中添加一些共享库.在这一刻,我想只运行程序(只有主要功能),它将能够添加我的外部库libAPIenergy.so的任何功能.我从这个论坛尝试了一些解决方案,但任何人都没有帮助.
下面我将介绍给出最少错误的解决方案.
在主要功能我包括
#include "APIenergy.h"
Run Code Online (Sandbox Code Playgroud)
CMake文件
cmake_minimum_required(VERSION 3.3)
project(TestProject)
add_library( libAPIenergy SHARED IMPORTED )
link_directories (/home/I/Lib/Linux/x86)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lAPIenergy ")
set(SOURCE_FILES main.cpp APIenergy.h)
add_executable(TestProject ${SOURCE_FILES})
Run Code Online (Sandbox Code Playgroud)
和错误:
/home/I/clion-1.2/bin/cmake/bin/cmake --build /home/I/.CLion12/system/cmake/generated/9faec492/9faec492/Debug --target TestProject -- -j 8
[ 50%] Building CXX object CMakeFiles/TestProject.dir/main.cpp.o
[100%] Linking CXX executable TestProject
/usr/bin/ld: cannot find -lAPIenergy
collect2: error: ld returned 1 exit status
CMakeFiles/TestProject.dir/build.make:94${PROJECT_SOURCE_DIR}/P2PTunnelAPIs.h.in": polecenia dla obiektu 'TestProject' nie powiod?y si?
make[3]: *** [TestProject] B??d 1
CMakeFiles/Makefile2:67: polecenia dla obiektu 'CMakeFiles/TestProject.dir/all' nie powiod?y …Run Code Online (Sandbox Code Playgroud)