Waf链接本地共享库(不编译lib)
Linking local shared library with Waf (no lib compilation)
我正在尝试构建一个简单的 c++11
程序 link 使用 waf-1.8.8 的共享库(动态库)。
特殊之处在于我不是要构建这个库,因为我已经有了开发版本,这意味着声明 (.hpp headers) 和定义 (.so
文件在 linux
下,.dll
+ .lib
在 windows 下)。
我只是想 link 那样:
$ g++ src/main.cpp -o mySFMLprogram.bin -std=c++11 -IthirdParty/SFML-2.2/include -LthirdParty/SFML-2.2/lib -lsfml-graphics -lsfml-system -lsfml-window
在我的项目的基本目录中效果很好。
用 wscript / waf 系统做这个是行不通的……我不知道如何做等同于 -LthirdParty/SFML-2.2/lib
(给 link 一个新的搜索目录呃,man gcc
说 Add directory dir to the list of directories to be searched for -l.
).
这是我项目的简化树:
$ tree
.
├── src
│ ├── main.cpp
│ └── wscript
├── thirdParty
│ ├── SFML-2.2
│ │ ├── include
│ │ │ └── SFML
│ │ │ ├── Audio
│ │ │ │ └── **.hpp
│ │ │ ├── Audio.hpp
│ │ │ ├── Config.hpp
│ │ │ ├── Graphics
│ │ │ │ └── **.hpp
│ │ │ ├── Graphics.hpp
│ │ │ ├── Main.hpp
│ │ │ ├── Network
│ │ │ │ └── **.hpp
│ │ │ ├── Network.hpp
│ │ │ ├── OpenGL.hpp
│ │ │ ├── System
│ │ │ │ ├── **.hpp
│ │ │ │ └── **.inl
│ │ │ ├── System.hpp
│ │ │ ├── Window
│ │ │ │ └── **.hpp
│ │ │ └── Window.hpp
│ │ └── lib
│ │ ├── libsfml-audio.so -> libsfml-audio.so.2.2.0
│ │ ├── libsfml-audio.so.2.2.0
│ │ ├── libsfml-graphics.so -> libsfml-graphics.so.2.2.0
│ │ ├── libsfml-graphics.so.2.2.0
│ │ ├── libsfml-network.so -> libsfml-network.so.2.2.0
│ │ ├── libsfml-network.so.2.2.0
│ │ ├── libsfml-system.so -> libsfml-system.so.2.2.0
│ │ ├── libsfml-system.so.2.2.0
│ │ ├── libsfml-window.so -> libsfml-window.so.2.2.0
│ │ └── libsfml-window.so.2.2.0
│ └── wscript
├── waf
└── wscript
编辑:在@mkaes 第一次解释后更新我的文件
根 wscript
是:
#! /usr/bin/env python
# encoding: utf-8
import os
VERSION = "0.1"
APPNAME = "wafTest"
def options(opt):
opt.load('compiler_cxx')
def configure(cfg):
cfg.load('compiler_cxx')
cfg.LIB_SFML = ['sfml-graphics', 'sfml-system', 'sfml-window']
cfg.INCLUDES_SFML = ['thirdParty/SFML-2.2/include')]
cfg.LIBPATH_SFML = ['thirdParty/SFML-2.2/lib')]
cfg.check(
features='cxx cxxprogram',
cxxflags=['-std=c++11', '-Wall'],
)
def build(bld):
bld.recurse('src')
而 src
wscript
是:
#! /usr/bin/env python
# encoding: utf-8
def build(bld):
#EDIT removed bld(name = 'sfml-graphics', export_includes = '../thirdParty/SFML-2.2/include')
#EDIT removed bld(name = 'sfml-window', export_includes = '../thirdParty/SFML-2.2/include')
#EDIT removed bld(name = 'sfml-system', export_includes = '../thirdParty/SFML-2.2/include')
bld.program(
features='cxx cxxprogram', #is it an option ?
source='main.cpp',
target='app',
use = ['SFML'], #first try> compilation error, headers missing
includes = '../thirdParty/SFML-2.2/include' #second try> link error, lib missing
)
src/main.cpp
的源代码(很容易从SFML基本测试程序中获取):
#include <SFML/Graphics.hpp>
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
// window.draw(...);
// end the current frame
window.display();
}
return 0;
}
waf 命令的先前输出 ./waf clean configure build
:
'clean' finished successfully (0.004s)
Setting top to : /home/***/Documents/dev/wafTest
Setting out to : /home/***/Documents/dev/wafTest/build
Checking for 'g++' (C++ compiler) : /usr/bin/g++
Checking for compiler flags ['-std=c++11', '-Wall'] : yes
'configure' finished successfully (0.070s)
Waf: Entering directory `/home/***/Documents/dev/wafTest/build'
[1/2] Compiling src/main.cpp
[2/2] Linking build/src/app
src/main.cpp.4.o: dans la fonction « main »:
main.cpp:(.text+0x10f): référence indéfinie vers « sf::String::String(char const*, std::locale const&) »
main.cpp:(.text+0x12d): référence indéfinie vers « sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int) »
main.cpp:(.text+0x160): référence indéfinie vers « sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&) »
main.cpp:(.text+0x19b): référence indéfinie vers « sf::Window::close() »
main.cpp:(.text+0x1b4): référence indéfinie vers « sf::Window::pollEvent(sf::Event&) »
main.cpp:(.text+0x1c8): référence indéfinie vers « sf::Color::Black »
main.cpp:(.text+0x1d0): référence indéfinie vers « sf::RenderTarget::clear(sf::Color const&) »
main.cpp:(.text+0x1df): référence indéfinie vers « sf::Window::display() »
main.cpp:(.text+0x1ee): référence indéfinie vers « sf::Window::isOpen() const »
main.cpp:(.text+0x206): référence indéfinie vers « sf::RenderWindow::~RenderWindow() »
main.cpp:(.text+0x23f): référence indéfinie vers « sf::RenderWindow::~RenderWindow() »
main.cpp:(.text+0x270): référence indéfinie vers « sf::RenderWindow::~RenderWindow() »
collect2: error: ld returned 1 exit status
Waf: Leaving directory `/home/***/Documents/dev/wafTest/build'
Build failed
-> task in 'app' failed (exit status 1):
{task 140412749536784: cxxprogram main.cpp.4.o -> app}
['/usr/bin/g++', 'src/main.cpp.4.o', '-o', '/home/***/Documents/dev/wafTest/build/src/app', '-Wl,-Bstatic', '-Wl,-Bdynamic']
怎么了?
编辑2:
此示例已解决,可在 Github 上找到:https://github.com/Tyrben/SFMLProjectUsingWaf
谢谢@mkaes
解决此问题的一种方法是在配置阶段添加您的库。例如
cfg.env.LIB_SFML = ['sfml-graphics', 'sfml-system', 'sfml-window']
cfg.env.LIBPATH_SFML = [os.path.join(os.getcwd(), 'thirdParty/SFML-2.2/lib')]
cfg.env.INCLUDES_SFML = [os.path.join(os.getcwd(), 'thirdParty/SFML-2.2/include']
然后在您的构建中添加 SFML
作为 use
指令。
更新:
将您的构建更改为:
def build(bld):
bld.program(
features='cxx cxxprogram', #is it an option ?
source='main.cpp',
target='app',
use = ['SFML']
)
我正在尝试构建一个简单的 c++11
程序 link 使用 waf-1.8.8 的共享库(动态库)。
特殊之处在于我不是要构建这个库,因为我已经有了开发版本,这意味着声明 (.hpp headers) 和定义 (.so
文件在 linux
下,.dll
+ .lib
在 windows 下)。
我只是想 link 那样:
$ g++ src/main.cpp -o mySFMLprogram.bin -std=c++11 -IthirdParty/SFML-2.2/include -LthirdParty/SFML-2.2/lib -lsfml-graphics -lsfml-system -lsfml-window
在我的项目的基本目录中效果很好。
用 wscript / waf 系统做这个是行不通的……我不知道如何做等同于 -LthirdParty/SFML-2.2/lib
(给 link 一个新的搜索目录呃,man gcc
说 Add directory dir to the list of directories to be searched for -l.
).
这是我项目的简化树:
$ tree
.
├── src
│ ├── main.cpp
│ └── wscript
├── thirdParty
│ ├── SFML-2.2
│ │ ├── include
│ │ │ └── SFML
│ │ │ ├── Audio
│ │ │ │ └── **.hpp
│ │ │ ├── Audio.hpp
│ │ │ ├── Config.hpp
│ │ │ ├── Graphics
│ │ │ │ └── **.hpp
│ │ │ ├── Graphics.hpp
│ │ │ ├── Main.hpp
│ │ │ ├── Network
│ │ │ │ └── **.hpp
│ │ │ ├── Network.hpp
│ │ │ ├── OpenGL.hpp
│ │ │ ├── System
│ │ │ │ ├── **.hpp
│ │ │ │ └── **.inl
│ │ │ ├── System.hpp
│ │ │ ├── Window
│ │ │ │ └── **.hpp
│ │ │ └── Window.hpp
│ │ └── lib
│ │ ├── libsfml-audio.so -> libsfml-audio.so.2.2.0
│ │ ├── libsfml-audio.so.2.2.0
│ │ ├── libsfml-graphics.so -> libsfml-graphics.so.2.2.0
│ │ ├── libsfml-graphics.so.2.2.0
│ │ ├── libsfml-network.so -> libsfml-network.so.2.2.0
│ │ ├── libsfml-network.so.2.2.0
│ │ ├── libsfml-system.so -> libsfml-system.so.2.2.0
│ │ ├── libsfml-system.so.2.2.0
│ │ ├── libsfml-window.so -> libsfml-window.so.2.2.0
│ │ └── libsfml-window.so.2.2.0
│ └── wscript
├── waf
└── wscript
编辑:在@mkaes 第一次解释后更新我的文件
根 wscript
是:
#! /usr/bin/env python
# encoding: utf-8
import os
VERSION = "0.1"
APPNAME = "wafTest"
def options(opt):
opt.load('compiler_cxx')
def configure(cfg):
cfg.load('compiler_cxx')
cfg.LIB_SFML = ['sfml-graphics', 'sfml-system', 'sfml-window']
cfg.INCLUDES_SFML = ['thirdParty/SFML-2.2/include')]
cfg.LIBPATH_SFML = ['thirdParty/SFML-2.2/lib')]
cfg.check(
features='cxx cxxprogram',
cxxflags=['-std=c++11', '-Wall'],
)
def build(bld):
bld.recurse('src')
而 src
wscript
是:
#! /usr/bin/env python
# encoding: utf-8
def build(bld):
#EDIT removed bld(name = 'sfml-graphics', export_includes = '../thirdParty/SFML-2.2/include')
#EDIT removed bld(name = 'sfml-window', export_includes = '../thirdParty/SFML-2.2/include')
#EDIT removed bld(name = 'sfml-system', export_includes = '../thirdParty/SFML-2.2/include')
bld.program(
features='cxx cxxprogram', #is it an option ?
source='main.cpp',
target='app',
use = ['SFML'], #first try> compilation error, headers missing
includes = '../thirdParty/SFML-2.2/include' #second try> link error, lib missing
)
src/main.cpp
的源代码(很容易从SFML基本测试程序中获取):
#include <SFML/Graphics.hpp>
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
// window.draw(...);
// end the current frame
window.display();
}
return 0;
}
waf 命令的先前输出 ./waf clean configure build
:
'clean' finished successfully (0.004s)
Setting top to : /home/***/Documents/dev/wafTest
Setting out to : /home/***/Documents/dev/wafTest/build
Checking for 'g++' (C++ compiler) : /usr/bin/g++
Checking for compiler flags ['-std=c++11', '-Wall'] : yes
'configure' finished successfully (0.070s)
Waf: Entering directory `/home/***/Documents/dev/wafTest/build'
[1/2] Compiling src/main.cpp
[2/2] Linking build/src/app
src/main.cpp.4.o: dans la fonction « main »:
main.cpp:(.text+0x10f): référence indéfinie vers « sf::String::String(char const*, std::locale const&) »
main.cpp:(.text+0x12d): référence indéfinie vers « sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int) »
main.cpp:(.text+0x160): référence indéfinie vers « sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&) »
main.cpp:(.text+0x19b): référence indéfinie vers « sf::Window::close() »
main.cpp:(.text+0x1b4): référence indéfinie vers « sf::Window::pollEvent(sf::Event&) »
main.cpp:(.text+0x1c8): référence indéfinie vers « sf::Color::Black »
main.cpp:(.text+0x1d0): référence indéfinie vers « sf::RenderTarget::clear(sf::Color const&) »
main.cpp:(.text+0x1df): référence indéfinie vers « sf::Window::display() »
main.cpp:(.text+0x1ee): référence indéfinie vers « sf::Window::isOpen() const »
main.cpp:(.text+0x206): référence indéfinie vers « sf::RenderWindow::~RenderWindow() »
main.cpp:(.text+0x23f): référence indéfinie vers « sf::RenderWindow::~RenderWindow() »
main.cpp:(.text+0x270): référence indéfinie vers « sf::RenderWindow::~RenderWindow() »
collect2: error: ld returned 1 exit status
Waf: Leaving directory `/home/***/Documents/dev/wafTest/build'
Build failed
-> task in 'app' failed (exit status 1):
{task 140412749536784: cxxprogram main.cpp.4.o -> app}
['/usr/bin/g++', 'src/main.cpp.4.o', '-o', '/home/***/Documents/dev/wafTest/build/src/app', '-Wl,-Bstatic', '-Wl,-Bdynamic']
怎么了?
编辑2: 此示例已解决,可在 Github 上找到:https://github.com/Tyrben/SFMLProjectUsingWaf 谢谢@mkaes
解决此问题的一种方法是在配置阶段添加您的库。例如
cfg.env.LIB_SFML = ['sfml-graphics', 'sfml-system', 'sfml-window']
cfg.env.LIBPATH_SFML = [os.path.join(os.getcwd(), 'thirdParty/SFML-2.2/lib')]
cfg.env.INCLUDES_SFML = [os.path.join(os.getcwd(), 'thirdParty/SFML-2.2/include']
然后在您的构建中添加 SFML
作为 use
指令。
更新: 将您的构建更改为:
def build(bld):
bld.program(
features='cxx cxxprogram', #is it an option ?
source='main.cpp',
target='app',
use = ['SFML']
)