so that's how libraries work

This commit is contained in:
Nicholas O'Connor 2017-03-23 12:06:29 -07:00
parent 64c96cba26
commit 30d0787a62
5 changed files with 45 additions and 5 deletions

View File

@ -35,9 +35,11 @@ foreach(cogfile ${cogfiles})
set(coggedfiles ${coggedfiles} "${thisfile}")
endforeach(cogfile)
add_library(bottles SHARED ${src}/bottles/bottles.cpp)
add_custom_target(cog ALL DEPENDS ${coggedfiles})
add_executable(cellar ${src}/cellar.cpp ${src}/commands.cpp ${src}/fs.cpp
${src}/bottles.cpp ${src}/version.cpp)
${src}/version.cpp)
add_dependencies(cellar cog)
target_link_libraries(cellar ${Boost_LIBRARIES})
target_link_libraries(cellar ${Boost_LIBRARIES} bottles)

View File

@ -5,6 +5,7 @@
#include <map>
#include <string>
#include "commands.hpp"
#include "json.hpp"
using namespace std;
@ -27,7 +28,10 @@ namespace cellar {
string canonical_path;
Bottle();
};
map<string, Bottle> get_bottles();
extern map<string, Bottle> get_bottles();
}
namespace commands {
extern map<string, cellar::commands::CommandFunction> bottles_commands();
}
}

24
include/dll.hpp Normal file
View File

@ -0,0 +1,24 @@
#if defined _WIN32 || defined __CYGWIN__
#ifdef BUILDING_DLL
#ifdef __GNUC__
#define DLL_PUBLIC __attribute__ ((dllexport))
#else
#define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
#endif
#else
#ifdef __GNUC__
#define DLL_PUBLIC __attribute__ ((dllimport))
#else
#define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
#endif
#endif
#define DLL_LOCAL
#else
#if __GNUC__ >= 4
#define DLL_PUBLIC __attribute__ ((visibility ("default")))
#define DLL_LOCAL __attribute__ ((visibility ("hidden")))
#else
#define DLL_PUBLIC
#define DLL_LOCAL
#endif
#endif

View File

@ -11,11 +11,13 @@
#include "bottles.hpp"
#include "commands.hpp"
#include "dll.hpp"
#include "fs.hpp"
using namespace std;
using namespace cellar::bottles;
using CommandFunction = cellar::commands::CommandFunction;
using json = nlohmann::json;
Bottle::Bottle() {
@ -25,7 +27,7 @@ Bottle::Bottle() {
type = bottle_anonymous;
}
map<string, Bottle> cellar::bottles::get_bottles() {
DLL_PUBLIC map<string, Bottle> cellar::bottles::get_bottles() {
map<string, Bottle> result;
string homepath = getenv("HOME");
@ -95,4 +97,8 @@ void print_bottles(int argc, char** argv) {
cout << endl;
}
}
cellar::commands::CommandFunction listcmd = cellar::commands::command_map["list"] = &print_bottles;
DLL_PUBLIC map<string, CommandFunction> cellar::commands::bottles_commands() {
map<string, CommandFunction> result;
result.insert(pair<string,CommandFunction>("list", &print_bottles));
return result;
}

View File

@ -37,6 +37,10 @@ int main(int argc, char* argv[]) {
cmdparse.parse(argc, argv);
for (auto item : commands::bottles_commands()) {
commands::command_map[item.first] = item.second;
}
string usercmd = command.getValue();
if (commands::command_map.count(usercmd) > 0) {
commands::command_map[usercmd](argc, argv);