Error when Call curlHelperSetOptString from module CCurl - swift -Kitura - swift

i use CCurl (https://github.com/IBM-Swift/CCurl.git) in my project (Kitura https://github.com/IBM-Swift/Kitura) then i call func curlHelperSetOptString , compile "swift build" and get an error:
duplicate symbol _curlHelperSetOptString in:
/Users/xxxx/Documents/server/ServerSwift/.build/debug/ServerSwift.build/UploadService.swift.o /Users/xxxx/Documents/server/ServerSwift/.build/debug/KituraNet.build/ClientRequest.swift.o
ld: 1 duplicate symbol for architecture x86_64 :0: error:
link command failed with exit code 1 (use -v to see invocation)
:0: error: build had 1 command failures
code:
import CCurl
var handle=curl_easy_init()
if (handle != nil) {
let url = "http: //example.com/"
let buffer=url.cString(using: .utf8)
curlHelperSetOptString(handle, CURLOPT_URL, buffer)
}
Help me ,plz

Actually it may be because we have defined the CCurl helper functions as extern inline, rather than as static inline. Apparently extern inline causes one of the references to the defined function to become an external name, which can cause problems if it is imported more than once.
We'll look into this.

IBM-Swift/CCurl.git 0.2.2 has been tagged. It contains a fix for the problem mentioned here.

You're probably including CCurl directly in your Package.swift when it's already included in Kitura-Net/Package.swift.
With most Swift modules, this wouldn't be a problem, but CCurl has to have a hack in it because libCurl contains mostly variadic functions and Swift doesn't import variadic functions from C libraries. The hack creates static functions in the C header file to create non-variadic version of the libCurl functions. It's those static functions that are being duplicated here (and each module is compiled separately, so you can't #ifndef around them because they can't see each other).
Try removing the CCurl dependency from your Package.swift file and just depend on the fact that it's being included for you, and hopefully you'll be okay.

Check that you don't have multiple entries under Build Phases/Compile Sources. If yes, remove them.
Also you could try cleaning your project or even run swift package generate-xcodeproj again.

It's because you are importing ccurl which is already imported in the kitura-net package.

Related

How to import library into swift code used in CLI?

What I want to do
I want to try to get son file through API by swift library on CLI.
I am using the library, Alamofire, but I can't understand how to import swift library.
Maybe some tasks are needed before import (building? linking?) but I can't find how to document...
Could you teach me how to do it?
Problem
$ swift getAPI.swift
getAPI.swift:1:8: error: no such module 'Alamofire'
import Alamofire
^
Sourcecode
import Alamofire
func getArticles(){
Alamofire.request(.GET, "https://qiita.com/api/v2/items")
.responseJSON { response in
print(response.result.value )
}
}
getArticles()
This should help with running one-off scripts and when using the Swift REPL. However, for building simple tools for the command line, I would recommend using the Swift Package Manager since it takes care of the linking and dependency management for you.
To link your script code to the Alamofire library, the swift compiler has to know where to the library is located. By default it searches /Library/Frameworks, but we can supply options to tell it where else to look. Checking swift --help, we see the following options (among many others).
USAGE: swift [options] <inputs>
OPTIONS:
-F <value> Add directory to framework search path
-I <value> Add directory to the import search path
-L <value> Add directory to library link search path
The directory you supply must to contain the compiled binaries of the libraries you want to import.
To import .swiftmodule binaries, use -I
To import .framework binaries, use -F
For linking to libraries like libsqlite3.0.dylib, use -L
I think Carthage and CocoaPods will build frameworks, whereas the Swift Package Manager will build .swiftmodules. All three should put the binaries in very predictable locations. Different kinds of binaries may all be in the same and that's okay.
Putting it all together, if you build with SPM, your invocation might look like this:
$ swift -I .build/debug/
But if you manage dependencies with Carthage, it might look something like this:
$ swift -F ./Carthage/Build/iOS
For further reading, I found these resources useful:
krakendev.io/blog/scripting-in-swift
realm.io/news/swift-scripting/
Update: The Swift package manager can now take care of this for you as well! If you're writing a script as a package and include a bunch of dependencies, like Alamofire, you can now test them out in the REPL. Just cd into your package directory and launch it using swift run --repl. See the swift.org blog for more details.

SwiftPM: How to setup Swift module.map referring to two connected C libraries

I'm trying to build a Swift Package Manager system package (a module.modulemap)
making available two system C libraries where one includes the other.
That is, one (say libcurl) is a base module and the other C library is including
that (like so: #include "libcurl.h"). On the regular C side this works, because
the makefiles pass in proper -I flags and all is good (and I could presumably
do the same in SPM, but I'd like to avoid extra flags to SPM).
So what I came up with is this module map:
module CBase [system] {
header "/usr/include/curl.h"
link "curl"
export *
}
module CMyLib [system] {
use CBase
header "/usr/include/mylib.h"
link "mylib"
export *
}
I got importing CBase in a Swift package working fine.
But when I try to import CMyLib, the compiler complains:
error: 'curl.h' file not found
Which is kinda understandable because the compiler doesn't know where to look
(though I assumed that use CBase would help).
Is there a way to get this to work w/o having to add -Xcc -I flags to the
build process?
Update 1: To a degree this is covered in
Swift SR-145
and
SE-0063: SwiftPM System Module Search Paths.
The recommendation is to use the Package.swift pkgConfig setting. This seems to work OK for my specific setup. However, it is a chicken and egg if there is no .pc file. I tried embedding an own .pc file in the package, but the system package directory isn't added to the PKG_CONFIG_PATH (and hence won't be considered during the compilation of a dependent module). So the question stands: how to accomplish that in an environment where there libs are installed, but w/o a .pc file (just header and lib).

Early grey environment setup

I'm trying to get Earlgrey setup on my computer by following the steps for Cocoapod installation as described here
After performing all the steps I keep on getting compilation error during build
EarlGrey.swift:17:27: Use of undeclared type 'EarlGreyImpl'
There are 13 such compilation errors all related to unresolved identifier.
I re-tried the steps multiple times with same results. My folder structure matches with what is shown in the instructions.
Any suggestions where should i look further to get this resolved.
For reference i'm using this swift project as base for writing test cases.
So the issue was related to BridgingHeader.h file setting in
test target -> build Setting -> Swift compiler -Code generation -> Objective-C bridging Header
I copied the file from demo project, but did not specify it in the above setting. Once specified there, the compilation errors was gone.
Try importing EarlGrey using #import <EarlGrey/EarlGrey.h> instead of #import EarlGrey;

Build and guard against missing library using Objective-C

I have a library (lib.a) and a header file (lib1.h). The problem is the library is too big and only a relatively small subset of users need it.
Would it be possible to build my Xcode project without the library? Currently I get:
ld: library not found for -llib
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Would it be possible to guard against usage of the library not existing inside the Xcode project? I found the following, but I'm not sure if it's sufficient (assuming I can get the project to build without the lib):
if ([MyLibABC class]) { ... } else { NSLog(#"Add Lib.a"); }
Side note:
In Java I would use reflection to call methods implemented in the library (lib.jar) and if the library is not present I would catch the ClassNotFoundException and show an error message or something (lib.jar missing, please consult the documentation).
I'm targeting >= iOS5.
Unlike desktop operating systems which support dynamic libraries, iOS supports only static libraries. The only way around this would be building two targets - one that uses the library, and the other one that does not.
You can build both targets from the same set of sources by using conditional compilation. In the version that conditionally compiles out the references to the "big library" there would be no references to it, so the linker would not complain about missing references.

Error running Simulator: duplicate symbol for architecture i386

I added ShareKit framework to try and here is the error running it in Simulator:
duplicate symbol _LFHRReadStreamClientCallBack in
.../Xcode/DerivedData/....build/Objects-normal/i386/LFHTTPRequest-8C6F35228BA446B9.o
and
.../Xcode/DerivedData/....build/Objects-normal/i386/LFHTTPRequest-8ACF920D803FDCA6.o
for architecture i386
I looked at previous posts Build Error - missing required architecture i386 in file and tried to edit project.pbxproj commenting FRAMEWORK_SEARCH_PATHS lines out as well as looking for .framework files accidently added to my project but with no luck.
What else can I try?
You've included LFHTTPRequest in your project twice. It's possible that another library (such as sharekit) included it for you, doublecheck the files sharekit includes and remove one copy of LFHTTPRequest and things should compile.
This can also happen if you move coredata-generated classes into a group, like "Models" and then regenerate the NSManagedObjectModel subclasses. The generated files will be placed in the project root and added into the build twice.
I got the same error when working with TessBaseAPI. I had two c++ files and both had the following declaration:
namespace tesseract {
class TessBaseAPI;
};
tesseract::TessBaseAPI *tesseract1;
uint32_t *pixels;
The I read the full error message. I got a line there:
duplicate symbol _tesseract1 in
And later, which files contains the duplicate also included (the file name).
So, I changed the instance name as follows:
namespace tesseract {
class TessBaseAPI;
};
tesseract::TessBaseAPI *tesseractNew;
uint32_t *pixelsNew;
That solved my problem.